九度oj 1034 寻找大富翁 2009年浙江大学计算机及软件工程研究生机试真题

题目1034:寻找大富翁

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:5323

解决:2123

题目描述:
    浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁.
输入:
    输入包含多组测试用例.
    每个用例首先包含2个整数n(0<n<=100000)和m(0<m<=10),其中: n为镇上的人数,m为需要找出的大富翁数, 接下来一行输入镇上n个人的财富值.
    n和m同时为0时表示输入结束.
输出:
    请输出乌镇前m个大富翁的财产数,财产多的排前面,如果大富翁不足m个,则全部输出,每组输出占一行.
样例输入:
3 1
2 5 -1
5 3
1 2 3 4 5
0 0
样例输出:
5
5 4 3
来源:
2009年浙江大学计算机及软件工程研究生机试真题
 1 #include <cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string>
 5 #include<cstring>
 6 #include<vector>
 7 using namespace std;
 8 struct node{
 9     int v;
10 };
11 bool operator<(const node &x, const node &y)//可以学习的地方,vector使用,降序排列 
12 //当return 的是1时,先输出左方的x,反之……记得加const 
13 {
14     return x.v > y.v;
15 }
16 int main(){
17     int n,m;
18     while(cin>>n>>m&&n&&m){
19         vector<node> v;
20         int i;
21         for(i=0;i<n;i++){
22             node d;
23             cin>>d.v;
24             v.push_back(d);
25         }
26         sort(v.begin(),v.end());
27         m=n>m?m:n;
28         cout<<v[0].v;
29         for(i=1;i<m;i++){
30             cout<<' '<<v[i].v;
31         }
32         cout<<endl; 
33     }
34     return 0;
35 }
原文地址:https://www.cnblogs.com/Deribs4/p/4292228.html