• 相邻数对 201409-1


    问题描述
      给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1。
    输入格式
      输入的第一行包含一个整数n,表示给定整数的个数。
      第二行包含所给定的n个整数。
    输出格式
      输出一个整数,表示值正好相差1的数对的个数。
    样例输入
    6
    10 2 6 3 7 8
    样例输出
    3
    样例说明
      值正好相差1的数对包括(2, 3), (6, 7), (7, 8)。
    评测用例规模与约定

      1<=n<=1000,给定的整数为不超过10000的非负整数。

    参考代码:

    #include <iostream>
    using namespace std;
    int main(){
    int n, result=0;
    cin >> n;
    int *t = new int[n];
    for (int i = 0; i < n;i++)
    {
    cin >> t[i];
    }
    for (int i = 0; i < n-1;i++)
    {
    for (int j = i+1; j < n;j++)
    {
    if ((t[i] - t[j]) == 1 || (t[i] - t[j]) == -1)
    {
    result++;
    }
    }
    }
    cout << result;
    return 0;
    }

    提交结果: 

    代码长度编程语言评测结果得分时间使用空间使用
    332B C++ 正确 100 0ms 504.0KB


  • 相关阅读:
    RESTful规范1
    Django -- 发送HTML格式的邮件
    11.10 vue
    Selenium 使用
    Beautiful Soup的用法
    Pthon常用模块之requests,urllib和re
    爬虫--工具安装Jupyter anaconda
    11-3
    Python -- tabulate 模块,
    Python -- queue队列模块
  • 原文地址:https://www.cnblogs.com/bao-ZhangJiao/p/14268806.html
Copyright © 2020-2023  润新知