forked from zhuli19901106/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ1047(AC).cpp
78 lines (73 loc) · 1.13 KB
/
POJ1047(AC).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
void reverse(char s[])
{
int i;
int len;
char t;
len = strlen(s);
i = 0;
while(i < len - 1 - i){
t = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = t;
++i;
}
}
int main()
{
char s[1000];
char t[1000];
char loop[1000];
int i, j, k, n;
while(scanf("%s", s) == 1){
n = strlen(s);
reverse(s);
strcpy(loop, s);
strcat(loop, s);
loop[2 * n - 1] = 0;
for(i = 0; i < n; ++i){
t[i] = s[i];
}
t[i] = 0;
for(i = 1; i < n; ++i){
for(j = 0; j < n; ++j){
t[j] = (s[j] - '0') + (t[j] - '0');
}
for(j = 0; j < n; ++j){
t[j + 1] += t[j] / 10;
t[j] %= 10;
t[j] += '0';
}
if(t[n]){
//overflow, not cyclic
break;
}else{
for(j = 0; j < n; ++j){
for(k = 0; k < n; ++k){
if(t[k] != loop[j + k]){
break;
}
}
if(k == n){
break;
}
}
if(j == n){
//no match, not cyclic
break;
}
}
}
reverse(s);
if(i == n){
printf("%s is cyclic\n", s);
}else{
printf("%s is not cyclic\n", s);
}
}
return 0;
}