HDU1280 前m大的数

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
Problem Description
还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就可以了。  
给定一个包含N(N<=3000)个正整数的序列,每个数不超过5000,对它们两两相加得到的N*(N-1)/2个和,求出其中前M大的数(M<=1000)并按从大到小的顺序排列。
 

Input
输入可能包含多组数据,其中每组数据包括两行:  
第一行两个数N和M,  
第二行N个数,表示该序列。

 

Output
对于输入的每组数据,输出M个数,表示结果。输出应当按照从大到小的顺序排列。
 

Sample Input
 
   
4 41 2 3 44 55 3 6 4
 

Sample Output
 
   
7 6 5 511 10 9 9 8
 


 

#include <iostream>#include <algorithm>#include <string.h>using namespace std;int cmp(const int a,const int b){    return a>b;}int a[3005],sum[3000*3000/2];int main(){    int n,m;    while(cin >> n >> m)    {        int i,j;        for(i = 0;i<n;i++)        {            cin >> a[i];        }        int k = 0;        for(i = 0;i<n-1;i++)        {            for(j = i+1;j<n;j++)            {                sum[k++] = a[i]+a[j];            }        }        sort(sum,sum+(n*(n-1))/2,cmp);        for(i = 0;i<m;i++)        {            if(!i)            cout << sum[i];            else            cout << " " << sum[i];        }        cout << endl;    }    return 0;}


 

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/htrdchh/article/details/84195273