#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> v(N);
for (int n = 0; n < N; n++) {
cin >> v[n];
}
sort(v.begin(), v.end(), [](int a, int b) -> bool { return a > b; });
int ans = 0;
while (ans < N && v[ans] > ans + 1) {
ans++;
}
cout << ans << endl;
}
/*
Sample Input:
10
6 7 6 9 3 10 8 2 7 8
Sample Output:
6
*/
下面的代码时错误的
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> v(N);
for (int n = 0; n < N; n++) {
cin >> v[n];
}
sort(v.begin(), v.end());
map<int, int> mp;
for (int i = 0; i < v.size(); i++) {
N--;
mp[v[i]] = N;
}
int ans = 0;
for (auto it : mp) {
// cout << it.first << " " << it.second << endl;
if (it.second >= it.first) {
ans = it.first;
}
}
cout << ans << endl;
}
/*
Sample Input:
10
6 7 6 9 3 10 8 2 7 8
Sample Output:
6
*/
当输入 5
个 5
时,该程序中 mp[5] = 0
,因此会输出 0
,实际应该输出 4
。(感谢 CC 的测试用例❤)