传送门:HDU 1280 前M大的数
分析:
哈希数组大小注意。因为所有的整数都不超过5000, 所以最大的和也不超过10000, 数组开10001即可!
#include <stdio.h>
#include <string.h>
int main() {
int hash[10001],arr[3001];
int N,M,i,j,cnt,flag;
while(scanf("%d%d",&N,&M) != EOF) {
memset(hash, 0, sizeof(hash));
cnt = 0;
for(i=0; i<N; i++)
scanf("%d",arr+i);
// std::sort(arr, arr+N, cmp); 桶已经编好序了 无需排序
for(i=0; i<N; i++
for(j=i+1; j<N; j++)
hash[arr[i]+arr[j]]++;
flag = 1;
for(i=10000; i>-1; i--) {
while(hash[i]--) {
if(flag)
printf("%d",i),flag=0;
else
printf(" %d",i);
cnt++;
if(cnt == M)
goto res;
}
}
res:
// printf("\n");
puts("");
}
return 0;
}