字符串:
AC代码1:
参考:https://blog.csdn.net/dumeichen/article/details/47746581
//字符串,模拟
#include <iostream>
#include <cstring>
using namespace std;
const int N = 65;
char s[N];
int d[100], tmp[100], len;
int test(int x) {
int i, j;
//从新数的第i位开始,循环判断
for (i = x, j = 0; j < len; i = (i + 1) % len, j++) {
if (tmp[i] != d[j])
return 0;
}
return 1;
}
int check(int x) {
int i, j;
memset(tmp, 0, sizeof(tmp));
for (i = j = 0; i < len; i++) {//字符串乘法,辅助数组存储结果
tmp[i] += d[i] * x;
tmp[i + 1] += tmp[i] / 10;//进位
tmp[i] %= 10;
}
if (tmp[i])//位数增加
return 0;
for (i = 0; i < len; i++) {//判断是否是,原数的循环
if (test(i))
return 1;
}
return 0;
}
int main() {
while (scanf("%s", s) != EOF) {
int i, j;
len = strlen(s);
for (i = 0; i < len; i++)//反转数组,存成int型
d[len - 1 - i] = s[i] - '0';
for (i = 2; i <= len; i++) {
if (!check(i))
break;
}
if(i<=len)
printf("%s is not cyclic\n", s);
else
printf("%s is cyclic\n", s);
}
return 0;
}
数学:
对于数num,如果其位数是n,num*(n+1)得到的结果是n个9,则这个数可循环
参考:https://blog.csdn.net/furney/article/details/7168724
AC代码2:
//数学
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string num;
bool flag;
int i, n, c, t;
while (cin >> num) {
flag = true;
n = num.size() + 1;//可能有进位
c = 0, t = 0;
for (i = n - 2; i >= 0; i--) {
t = num[i] - '0';
if ((t * n + c) % 10 != 9) {//判断结果是否全为9
flag = false;
break;
}
c = (t * n + c) / 10;//进位
}
if (flag)
cout << num << " is cyclic" << endl;
else
cout << num << " is not cyclic" << endl;
}
return 0;
}