题目描述

源代码
#include<iostream>
using namespace std;
int a[1005];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
int i, j;
int count = 0;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (abs(a[i] - a[j]) == 1)
{
count++;
}
}
}
cout << count << endl;
return 0;
}
关于这题
这里用的暴力法 比较两个数的差的 绝对值如果为1 符合条件 count++;

这是一个使用C++编写的程序,通过暴力遍历数组来寻找相邻数对,其差值的绝对值为1的计数。程序首先读取整数n,然后输入n个整数,接着通过两层循环检查所有数对,如果满足条件则增加计数count,最后输出符合条件的数对数量。
386

被折叠的 条评论
为什么被折叠?



