4 1 2 3 4 2 5 5
25 10HintFirstly small W takes any pair of 1 2 3 4 and add them, he will get 3 4 5 5 6 7. Then he deletes the repeated numbers, he will get 3 4 5 6 7, Finally he gets the sum=3+4+5+6+7=25.
题目的意思看了挺久的。,给你一个N,叫你输入N个数。然后从N个数中任意取两个数相加,得到一个新的数,将所有新的数求和就是答案。注意一点就好,相同的元素只算一个。所以去掉重复的就好。上代码
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int main() { int n,i,j; while(~scanf("%d",&n)) { int a[105]; int b[10005]; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); __int64 sum=0; int l=0; for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) for(j=i+1;j<n;j++) { b[l++]=a[i]+a[j]; } sort(b,b+l); int n1=unique(b,b+l)-b; //去掉相同的。此时新数组个数n1. for(i=0;i<n1;i++) sum+=b[i]; printf("%I64d\n",sum); } return 0; }
原文:http://blog.csdn.net/sky_miange/article/details/42172973