总算过了,看了题解才知道是因为重复的不算,比如 1 + 4、4 + 1、2 + 3、3 + 2 都是重复,也就是结果相同的不同的两个数只能出现一组。于是加上判断就好。
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define ll long long
#define N 200005
int gcd(int a, int b){return b == 0 ? a : gcd(a%b, a);}
int a[N], vis[N];
int top = 0;
int main(int n, char**v)
{
scanf("%d", &n);
for (int i = 0; i < N; vis[i++] = 0);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
vis[a[i]] = 1;
}
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (vis[a[i] + a[j]] && vis[a[i] + a[j]] != 2)
vis[a[i] + a[j]] = 2, top++;
printf("%d\n", top);
return 0;
}