注:重点在fz()函数
#include <bits/stdc++.h>
using namespace std;
char c[25], b[25], ans[25];
int len;
bool vis;
bool pd() {
for (int i = 1; i <= len; i++) {
if (b[i] > ans[i]) return 1;
if (b[i] < ans[i]) return 0;
}
return 0;
}
void zy() {
for (int i = 1; i <= len; i++) ans[i] = b[i];
}
void dfs(int d, int x, int dt) { // 现在是第d个数,前x个数为一循环
if (d == len + 1) {
//cout << (b+1) << "\n";
if (pd()) {
zy();
}
return ;
}
if (d > x) b[d] = b[(d - 1) % x + 1];
else {
b[d] = c[d] + dt;
}
if (b[d] < '0') return ;
if (b[d] > c[d] && !vis) return ;
if (b[d] < c[d]) vis = 1;
int vis1 = vis;
dfs(d + 1, x, 0);
vis = vis1;
if (d < x) dfs(d + 1, x, -1);
}
void fz(int dt) {
for (int i = 1; i <= len; i ++) {
ans[i] = '0';
b[i] = c[1] + dt;
}
b[len + 1] = '\0';
c[len + 1] = '\0';
cout << (b + 1) << " " << (c + 1) << " " << ((b + 1) > (c + 1)) << "\n";
if ((b + 1) <= (c + 1)) zy();
}
int main() {
cin.tie(0); ios::sync_with_stdio(0);
int T;
ans[0] = '.';
cin >> T;
while (T--) {
cin >> (c + 1);
//cout << (c + 1) << "\n";
len = strlen(c + 1);
//cout << len << ": ";
fz(-1);
fz(0);
ans[len + 1] = '\0';
for (int i = 2; i <= len / 2; i++) {
vis = 0;
if (len % i == 0) {
dfs(1, i, 0);
dfs(1, i, -1);
}
}
cout << (ans + 1) << "\n";
// for (int i = 1; i <= len+1; i++) cout << ans[i];
// cout << "\n";
}
}
/*
1
303
*/
数据:
1
303
cout << (b + 1) << " " << (c + 1) << " " << ((b + 1) > (c + 1)) << "\n";
这一行的意思是,输出 b[1] ~ b[len + 1] , c[1] ~ c[len + 1] 和 (b 是否大于 c)。
相应的输出是:
222 303 1
333 303 1
也就是说 222 比 303 大,333 比 303 大。
所以为什么会出现这样的错误呢?qwq