原题链接:1117 Eddington Number (25分)
关键词:排序、枚举
British astronomer Eddington liked to ride a bike. It is said that in order to show off his skill, he has even defined an “Eddington number”, E – that is, the maximum integer E such that it is for E days that one rides more than E miles. Eddington’s own E was 87.
Now given everyday’s distances that one rides for N days, you are supposed to find the corresponding E (≤N).
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤105), the days of continuous riding. Then N non-negative integers are given in the next line, being the riding distances of everyday.
Output Specification:
For each case, print in a line the Eddington number for these N days.
Sample Input:
10
6 7 6 9 3 10 8 2 7 8
Sample Output:
6
题目大意: 给出一堆数,请你找出满足有n个数大于n的最大值。
分析: 题目理解了就不难,其实是个逻辑题。要找出对数组arr[]进行从大到小排列,如果满足arr[i]<i,往下循环,直到找到第一个不满足的情况。因为如果第3天的值小于3,那么再往后看就比3还小,那么答案就是2。
代码:
#include <iostream>
#include <algorithm>
using namespace std;const int maxn = 1e5 + 10;
int arr[maxn];bool cmp(int a, int b){return a > b;
}int main() {int n;cin >> n;for (int i = 1; i <= n; i++) cin >> arr[i];//递减排序,保证前e天的里程数都大于esort(arr + 1, arr + 1 + n, cmp);int e = 1;while (e <= n && arr[e] > e) e++; // e为天数, 第e天里程数 > e printf("%d", e-1); // e不满足条件退出循环, e - 1满足条件return 0;
}