寻找大富翁
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4574 Accepted Submission(s): 1857
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
Recommend
notonlysuccess | We have carefully selected several similar problems for you: 3786 3782 3784 3783 3789
1 #include <queue> 2 #include <cstdio> 3 #include <iostream> 4 using namespace std; 5 int main() 6 { 7 int n, m, i, p; 8 9 while(~scanf("%d %d", &n, &m), m+n) 10 { 11 12 priority_queue<int, vector<int>, less<int> >q; 13 for(i=0; i<n; i++) 14 { 15 scanf("%d", &p); 16 q.push(p); 17 } 18 printf("%d",q.top()); 19 q.pop(); 20 int total = 1; 21 while(!q.empty()) 22 { 23 if(total == m) 24 break; 25 printf(" %d",q.top()); 26 q.pop(); 27 total++; 28 } 29 printf("\n"); 30 31 } 32 return 0; 33 }
时间: 2024-10-20 09:34:06