BestCoder8 1001.Summary(hdu 4989) 解题报告

时间:2024-01-17 11:24:38

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4989

题目意思:给出 n 个数,然后将这些数两两相加,得到 n*(n-1) /2 对和,把重复的和去掉,最后相加起来。

  用STL中的set可以好方便的做出来,因为 insert 的时候它会自动去除重复的。记得要用 long long 或 int64,因为 -1000000000 <= ai <= 1000000000 !

  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
using namespace std; typedef __int64 LL;
const int maxn = + ;
LL a[maxn];
set<LL> s;
set<LL>::iterator ps; int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
for (int i = ; i <= maxn; i++)
s.clear();
for (int i = ; i < n; i++)
scanf("%I64d", &a[i]);
for (int i = ; i < n; i++)
{
for (int j = i+; j < n; j++)
s.insert(a[i] + a[j]);
}
LL ans = ;
for (ps = s.begin(); ps != s.end(); ps++)
ans += *ps;
printf("%I64d\n", ans);
}
return ;
}