http://acm.hdu.edu.cn/showproblem.php?pid=1280
题意:给定的N个整数序列, 两两求和,从大到小输出M个和数。因为所有整数不超过5000,则相加不会超过10000,可以用哈希解决。
#include <iostream>
using namespace std;
#define NUM_MAX 10003
#define N 3003
int num[N],total[NUM_MAX];
int main(){
int n,m,i,j;
while (scanf("%d%d",&n,&m)!=EOF){
memset(total,0,sizeof(total));
for (i=0;i<n;i++)
scanf("%d",&num[i]);
for (i=0;i<n;i++)
for (j=i+1;j<n;j++)
total[num[i]+num[j]]++;
for (i=NUM_MAX;i>=0;i--){
if (total[i]){
printf("%d",i);
m--;
total[i]--;
i++;
if (m==0){
printf("\n");
break;
}
printf(" ");
}
}
}
return 0;
}