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

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

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

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

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <set>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 typedef __int64 LL;
 9 const int maxn = 100 + 5;
10 LL a[maxn];
11 set<LL> s;
12 set<LL>::iterator ps;
13 
14 int main()
15 {
16     int n;
17     while (scanf("%d", &n) != EOF)
18     {
19         for (int i = 0; i <= maxn; i++)
20             s.clear();
21         for (int i = 0; i < n; i++)
22             scanf("%I64d", &a[i]);
23         for (int i = 0; i < n; i++)
24         {
25             for (int j = i+1; j < n; j++)
26                 s.insert(a[i] + a[j]);
27         }
28         LL ans = 0;
29         for (ps = s.begin(); ps != s.end(); ps++)
30             ans += *ps;
31         printf("%I64d
", ans);
32     }
33     return 0;
34 }
原文地址:https://www.cnblogs.com/windysai/p/4298498.html