B题差一个样例,急
我的思路挺奇怪的,不知道哪里错了,统计词频,然后词频大于1就返回错误,要特判n=1的情况,除了这个情况,n至少等于2,那就修改第一个位置为arr[1]。
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
map<int, int> m;
vector<int> arr(n + 1);
for (int i = 1; i <= n; i++) {
cin >> arr[i];
m[arr[i]]++;
}
sort(arr.begin(), arr.end());
if (n == 1) {
cout << 0 << endl;
} else {
bool flag = true;
for (auto& mm : m) {
if (mm.second > 1) {
flag = false;
cout << 0 << endl;
break;
}
}
if (flag) {
cout << 1 << endl;
cout << 1 << " " << arr[1] << endl;
}
}
return 0;
}
查看7道真题和解析