PAT A 1117. Eddington Number
https://www.patest.cn/contests/pat-a-practise/1117
首先题目意思就难懂,就是N个数,找一个数E,使得这N个数里面有E个数比E大
特别注意是大,相等不行
知道了这个
首先排序咯
然后找就好了
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N;
scanf("%d", &N);
vector<int> a;
for (int i = 0; i < N; i++) {
int x;
scanf("%d", &x);
a.push_back(x);
}
sort(a.begin(), a.end(), greater<int>());
int ans = 0;
for (int i = 0; i < N; i++) {
if (i + 1 < a[i]) {
ans = i + 1;
} else {
break;
}
}
cout << ans << endl;
}
本文介绍PAT A1117题目的解题思路及C++实现代码。题目要求从N个数中找到一个数E,使得恰好有E个数大于E,且不包含等于E的数。通过排序和遍历数组的方法找到满足条件的最大E值。
2659

被折叠的 条评论
为什么被折叠?



