1034.尋找大富翁

來源:互聯網
上載者:User

標籤:int   輸入   pre   std   整數   ace   span   main   namespace   

題目描述:
    浙江桐鄉烏鎮共有n個人,請找出該鎮上的前m個大富翁.
輸入:
    輸入包含多組測試案例.
    每個用例首先包含2個整數n(0<n<=100000)和m(0<m<=10),其中: n為鎮上的人數,m為需要找出的大富翁數, 接下來一行輸入鎮上n個人的財富值.
    n和m同時為0時表示輸入結束.
輸出:
    請輸出烏鎮前m個大富翁的財產數,財產多的排前面,如果大富翁不足m個,則全部輸出,每組輸出佔一行.
範例輸入:
3 12 5 -15 31 2 3 4 50 0
範例輸出:
55 4 3
#include<iostream>#include<algorithm>using namespace std;int main(){    int i,n,m;    int a[100001];    while(cin>>n>>m){        if(n==0 && m==0) break;        for(i=0;i<n;i++){            cin>>a[i];        }        sort(a,a+n);        bool first=true;        if(n<=m){            for(i=n-1;i>=0;i--){                if(first) {                    first=false;                    cout<<a[i];                }                else cout<<" "<<a[i];            }            cout<<endl;        }        else {            for(i=n-1;m>0;m--){                if(first){                    first=false;                    cout<<a[i];                }                else cout<<" "<<a[i];                i--;            }            cout<<endl;        }    }    return 0;} 

 

1034.尋找大富翁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.