码迷,mamicode.com
首页 > 其他好文 > 详细

前m大的数

时间:2014-11-07 12:38:59      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   os   sp   for   strong   

 

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
 
源代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
struct p{
    int x;
}digit[1000000];
//如果直接定义成digit[1000000]会溢出。但是用结构体则不会溢出
//为什么?
bool cmp(p a,p b)
{
    return a.x>b.x;
}
int main()
{
    int M,N;
    while(scanf("%d%d",&N,&M)!=EOF){
        int i;
        for(i=0;i<N;i++)
            scanf("%d",&digit[i].x);
        sort(digit,digit+N,cmp);
        for(i=0;i<M;i++){
            if(i==0)
                printf("%d",digit[i].x);
            else
                printf(" %d",digit[i].x);
        }
        printf("\n");
    }
    return 0;
}

心得:

为啥这么坑???为什么定义成结构体会AC?而直接用数组则不行?

前m大的数

标签:des   style   blog   io   color   os   sp   for   strong   

原文地址:http://www.cnblogs.com/yanglingwell/p/4080768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!