HDU-3785 Looking for Monopoly

Description

There are n people in Wuzhen, Tongxiang, Zhejiang, please find the top m rich men in the town.

Input

The input contains multiple sets of test cases.
Each use case first contains 2 integers n (0 <n <= 100000) and m (0 <m <= 10), where: n is the number of people in the town and m is what needs to be found The number of millionaires, enter the wealth value of n people in the town on the next line. When both
n and m are 0, it means the end of the input.

Output

Please output the number of properties of the first m rich men in Wuzhen. If there are less than m rich men, then all will be output. Each group of output will occupy one row.

Sample Input

3 1
2 5 -1
5 3
1 2 3 4 5
0 0

Sample Output

5
5 4 3
#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

bool cmp(int a, int b){
    return a > b;
}
int a[100005];

int main()
{
    int n, m;
    while (~scanf("%d %d", &n, &m) && !(n==0&&m==0)){
        for (int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        if (n > m){
            for (int i = 0; i < m; i++)
                for (int j = i+1; j < n; j++)
                    if (a[i] < a[j])
                        swap(a[i], a[j]);
            printf("%d", a[0]);
            for (int i = 1; i < m; i++)
                printf(" %d", a[i]);
        } else {
            sort(a, a+n, cmp);
            printf("%d", a[0]);
            for (int i = 1; i < n; i++)
                printf(" %d", a[i]);
        }
        printf("\n");
    }
    return 0;
}

 

Published 339 original articles · praised 351 · 170,000 views

Guess you like

Origin blog.csdn.net/Aibiabcheng/article/details/105353876