3561. 数螃蟹

shwei

//总体思路:确定公差,然后模拟结果

include

using namespace std;

int main(void) {
int n;
cin >> n;
int d;
int max = 0;
vector vec, res;
map hash;
while (n–) {
int num;
cin >> num;
vec.push_back(num);
}
//利用map找到公差
int len = vec.size()
for (int i = 1; i < len; i++) {
hash[vec[i] - vec[i - 1]]++;
}
for (map :: iterator it = hash.begin(); it != hash.end(); it++) {
if (it -> second > max) {
max = it -> second;
d = it -> first;
}
}
//确定首项
int i;
for (i = 1; i < len; i++) {
if (vec[i] - vec[i - 1] == d) {
long long one = vec[i] - i * d;
res.push_back(one);
for (int j = 1; j < vec.size(); j++) {
res.push_back(array1 + j * d);
}
//比较选取这个i后,是否满足不同数最多只有三个的要求
int cnt = 0;
for (int k = 0; k < len; k++) {
if (vec[k] != res[k]) {
cnt++;
}
}
if (cnt > 3) {
res.clear();
continue;
} else {
break;
}
}
}
输出结果
long long one = vec[i] - i * d;
for (int i = 0; i < len; i++) {
cout << one + i * d << ” “;
}
cout << endl;
return 0;
}

你当前正在回复 博客/题目
存在问题!