11. Adjacent number pairs

  1. Adjacent number pairs
    Problem description
    Given n different integers, ask how many pairs of integers there are, whose values ​​differ by exactly 1.
    Input format
    The first line of input contains an integer n, which represents the number of given integers.
    The second line contains the given n integers.
    Output format
    Output an integer, representing the number of pairs whose values ​​differ by exactly 1.
    Sample input
    6
    10 2 6 3 7 8
    Sample output
    3
    Sample description
    The pairs whose values ​​differ by exactly 1 include (2, 3), (6, 7), (7, 8).
    Evaluation use case scale and convention
    1<=n<=1000, the given integer is a non-negative integer not exceeding 10,000.
#include<bits/stdc++.h>

using namespace std;

int main()
{
    
    
    int i, j, n, t = 0;
    int a[1001];
    cin>>n;
    for (i = 0; i < n; i++)
        cin>>a[i];
    for (i = 0; i < n - 1; i++)
    {
    
    
        for (j = i + 1; j < n; j++)
        {
    
    
            if (abs(a[i] - a[j]) == 1) t++;
        }
    }
    cout<<t;
    return 0;
}

Guess you like

Origin blog.csdn.net/KO812605128/article/details/113447890