hdu 1425 sort

Problem Description
给你n个整数,请按从大到小的顺序输出其中前m大的数。
 
Input
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。
 
Output
对每组测试数据按从大到小的顺序输出前m大的数。
 
Sample Input
5 3 3 -35 92 213 -644
 
Sample Output
213 92 3
 
//注意,不能使用cout和cin会超时
 
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int data[10000004];

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        for(int i=0;i<n;i++)
        scanf("%d",&data[i]);
        sort(data,data+n);
        n--;
        for(int j=0;j<m-1;j++)
        {
            printf("%d ",data[n]);
            n--;
        }
        printf("%d\n",data[n]);
    }
    return 0;
}

 

posted @ 2016-03-22 21:47  邻家那小孩儿  阅读(145)  评论(0编辑  收藏  举报