PAT --- 1060.爱丁顿数 (25 分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MissXy_/article/details/88100417

1060 爱丁顿数 (25 分)

英国天文学家爱丁顿很喜欢骑车。据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数” EEE ,即满足有 EEE 天骑车超过 EEE 英里的最大整数 EEE。据说爱丁顿自己的 EEE 等于87。

现给定某人 NNN 天的骑车距离,请你算出对应的爱丁顿数 EEE≤N\le NN)。

输入格式:

输入第一行给出一个正整数 NNN (≤105\le 10^5105),即连续骑车的天数;第二行给出 NNN 个非负整数,代表每天的骑车距离。

输出格式:

在一行中给出 NNN 天的爱丁顿数。

输入样例:

10
6 7 6 9 3 10 8 2 7 8

输出样例:

6

思路

从下标1开始存储n天的公⾥里里数在数组num中,对n个数据从⼤大到⼩小排序,i表示了了骑⻋车的天数,那么
满⾜足num[i] > i的最⼤大值即为所求

代码

#include <iostream>
#include <algorithm>
using namespace std;

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

int main()
{	
	int n, num[100000];
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> num[i];
	sort(num + 1, num + n + 1, cmp); //从大到小排列
	int ans = 0, p = 1;
	while (ans <= n && num[p] > p) {
		ans++; p++;
	}
	cout << ans;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/MissXy_/article/details/88100417
今日推荐