算法1
枚举出所有的两两组合,然后来判断集合中的数是否满足两数之和;
可以申请一个bool数组,然后来存两数之和,然后最后循环判断每一个数是否有除本身之外的两数之和
巧妙点:
1)我们在算两数组合的时候的嵌套循环那个地方;
2)就是我们用bool数组来直接存我们计算出的两数和,为我们在最后判断有多少个的时候埋下了伏笔
3)for循环嵌套实现所有的两两数的组合,存在st数组里,预处理
C++ 代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n;
int a[N];
bool st[20010];
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
for (int i = 0; i < n; i ++ )
for (int j = 0; j < i; j ++ )
st[a[i] + a[j]] = true;
int res = 0;
for (int i = 0; i < n; i ++ ) res += st[a[i]];
printf("%d\n", res);
return 0;
}