SDNU 1060

1060.找第K大数
Time Limit: 1000 MS Memory Limit: 32768 KB
Total Submission(s): 761 Accepted Submission(s): 133
Description
给定 n(1 <= n <= 10000000) 个正整数(<= 2147483647),找出其中**的第K(1 <= K <= 10)大数。
Input**
第一行,两个整数n, K,第二行n个整数
Output
第K大数
Sample Input

5 3
10 15 6 8 3

Sample Output

8

Source
Unknown

#include<cstdio>
#include<algorithm>
using namespace std;
bool cmp(int  x,int y)
{
    if(x!=y)return x>y;//由大到小排列
}
int main()
{
    int m, n, b;
    int a[11]={0};
    scanf("%d %d", &n, &m);
    for(int i=0; i<n; i++)
    {
        scanf("%d", &b);
        if(b>a[9])//题目中n 的范围太大,每输入一个数与a[9]比较
            a[9]=b;
        sort (a, a+10, cmp);// 保证a[9]是最小的
    }
    printf("%d\n", a[m-1]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liudazhuang98/article/details/78606778
今日推荐