pat甲级 1117 Eddington Number (25分)
note:现场定义题。“Eddington number”, E – that is, the maximum integer E such that it is for E days that one rides more than E miles。
意思是,n个数中大于k的个数恰好为k个,k即为"Eddington number"。把数字按从大到小排序,则a[i]>i,说明序列a中存在i个数大于i,i的最大值即为"Eddington number"。有兴趣的可以自行动手模拟。
代码:
#include <iostream>
#include <algorithm>
using namespace std;
int num[100005];
int main()
{
int n,j=1;
cin>>n;
for(int i=1;i<=n;++i) cin>>num[i];
sort(num+1,num+n+1,[](int a,int b){return a>b;});//compare函数也可以写成greater<int>()
while(j<=n&&num[j]>j) ++j;
cout<<j-1<<endl;
}