PAT-A1117/B1060 Eddington Number/爱丁顿数 题目内容及题解

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).

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

现给定某人 N 天的骑车距离,请你算出对应的爱丁顿数 E(≤N)。

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​^5​​), the days of continuous riding. Then N non-negative integers are given in the next line, being the riding distances of everyday.

输入第一行给出一个正整数 N (≤10​5​​),即连续骑车的天数;第二行给出 N 个非负整数,代表每天的骑车距离。

Output Specification:

For each case, print in a line the Eddington number for these N days.

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

Sample Input:

10
6 7 6 9 3 10 8 2 7 8

Sample Output:

6

解题思路

  1. 读入数据;
  2. 将其按照从大到小的顺序排列;
  3. 从前到后遍历,找到某数大于其位置(从1开始)时退出;
  4. 输出结果并返回零值。

代码

#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 100010

int N;
int num[maxn];
bool cmp(int a,int b){
    return a>b;
}

int main(){
    int i;
    scanf("%d",&N);
    for(i=0;i<N;i++){
        scanf("%d",&num[i]); 
    }
    sort(num,num+N,cmp);
    for(i=0;1;i++){
        if(num[i]<=i+1){
            break;
        }
    }
    printf("%d\n",i);
    return 0;
}

运行结果

PAT-A1117/B1060 Eddington Number/爱丁顿数 题目内容及题解