考虑一组n个不同的正整数 a 1 , a 2 , . . . , a m a1,a2,...,am a1,a2,...,am,它们的值在 1 1 1到 1000000 1000000 1000000之间。给定一个整数 x x x
写一个程序 s u m x sumx sumx计算这样的数对个数 ( a i , a j ) (ai,aj) (ai,aj), 1 < = i < j < = n 1<=i
标准输入的第一行是一个整数 n ( 1 < = n < = 1000000 ) n(1<=n<=1000000) n(1<=n<=1000000) 第二行有 n n n个整数表示元素。第三行是一个整数 x ( 1 < = x < = 2000000 ) x(1<=x<=2000000) x(1<=x<=2000000)
输出一行包含一个整数表示这样的数对个数。
注意:对于 50 % 50\% 50%的测试数据, n < = 1000 n<=1000 n<=1000
sumx.in
9
5 12 7 10 9 1 2 3 11
13
sumx.out
3
不同的和为 13 13 13的数对是 ( 12 , 1 ) , ( 10 , 3 ) (12, 1), (10, 3) (12,1),(10,3)和 ( 2 , 11 ) (2, 11) (2,11)
这题我们直接根据题目来枚举然后判断即可
#include
#include
#include
#include
using namespace std;
int n, x, ans;
int a[100001];
int main()
{
freopen("sumx.in","r",stdin);
freopen("sumx.out","w",stdout);
scanf("%d",&n);
for(int i = 1; i <= n; ++i)
scanf("%d",&a[i]);
scanf("%d",&x);
for(int i = 1; i <= n; ++i)
for(int j = i + 1; j <= n; ++j)
if(a[i] + a[j] == x) ans++;
printf("%d",ans);
return 0;
}