hdu 1280 用sort函数解决。(转载)
#include<iostream>//2272848 2010-03-31 18:44:28 Accepted 1280 656MS 17868K 609 B C++ 悔惜晟
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX = 4498501;
struct stu
{
int sum;
}df[MAX];
int cmp(stu a, stu b)
{
return a.sum > b.sum;
}
int main()
{
int n, m, a[3001];
while(scanf("%d %d", &n, &m) != EOF)
{
int i, j;
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
int count =0;
for(i = 0; i < n - 1; i++)
for(j = i+1; j < n ;j++)
df[count++].sum = a[i] + a[j];
sort(df,df+count, cmp);
for(i = 0 ; i < m ; i++)
if(i != m - 1)
printf("%d ", df[i].sum);
else
printf("%d\n", df[i].sum);
}
}
posted on 2011-04-14 17:43 more think, more gains 阅读(152) 评论(0) 编辑 收藏 举报