2018北大暑校acm算法训练课程 输出前k大的数 排序sort

总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 65536kB
描述
给定一个数组,统计前k大的数并且把这k个数从大到小输出。

输入
第一行包含一个整数n,表示数组的大小。n < 100000。
第二行包含n个整数,表示数组的元素,整数之间以一个空格分开。每个整数的绝对值不超过100000000。
第三行包含一个整数k。k < n。
输出
从大到小输出前k大的数,每个数一行。
样例输入
10
4 5 6 9 8 7 1 2 3 0
5
样例输出
9
8
7
6
5

本题贡献了整个小组的1wa 非常荣幸,又感到开心,混合输入这个坑,我可真是去**的

除了这也没啥说的 ,签到题

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;
#define eps 1e-7
//#define Kongxiangzhouye
#define _for(i,a,b) for( int i=(a); i<(b); ++i)
#define _rep(i,a,b) for( int i=(a); i<=(b); ++i)
int readint(){int x;scanf("%d",&x);return x;}
struct cmpFunctor{
    inline bool operator ()(const int& t1,const int& t2){
        return t1>t2;
    }
};
int a[101010];
int main(){
    //混合输入? 
    ios::sync_with_stdio(false);
    int n;
    while(scanf("%d",&n)!=EOF){
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++){
            a[i]=readint();
        }
        sort(a,a+n,cmpFunctor());
        int m=readint();
        for(int i=0;i<m;i++){
            printf("%d\n",a[i]);
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38842456/article/details/81271282