寻找大富翁

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2248    Accepted Submission(s): 1081


Problem Description
浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁.
 

 

Input
输入包含多组测试用例.
每个用例首先包含2个整数n(0<n<=100000)和m(0<m<=10),其中: n为镇上的人数,m为需要找出的大富翁数, 接下来一行输入镇上n个人的财富值.
n和m同时为0时表示输入结束.
 

 

Output
请输出乌镇前m个大富翁的财产数,财产多的排前面,如果大富翁不足m个,则全部输出,每组输出占一行.
 

 

Sample Input
3 1 2 5 -1 5 3 1 2 3 4 5 0 0
 

 

Sample Output
5 5 4 3
 

 

Source
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <queue>
 6 
 7 using namespace std;
 8 
 9 void search(int n, int m)
10 {
11     priority_queue <int> q;  // 默认就是从大到小排序的 
12     int t; 
13     for(int i = 0; i < n; ++i)
14     {
15         scanf("%d", &t);
16         q.push(t);
17     }
18     m = m > n ? n : m;
19     for(int i = 0; i < m-1; ++i)
20     {
21         printf("%d ", q.top());
22         q.pop();
23     }
24     printf("%d\n", q.top());
25 }
26 
27 int main()
28 {
29     int n, m;
30     while(scanf("%d%d", &n, &m), m||n)
31     {
32         search(n, m);
33     }
34     return 0;
35 }

 

 
posted on 2013-06-29 20:47  可笑痴狂  阅读(452)  评论(0编辑  收藏  举报