枚举下,再验证,高精度乘法那需要处理好,0可以在最前端
#include <stdio.h>
#include <string.h>
char str[100];
char num[100];
void turn(int len)
{
int mid=len>>1;
char temp;
for ( int i= 0; i < mid; i++ )
{
temp= str[i];
str[i]=str[len-1-i];
str[len-1-i]= temp;
}
}
int calu(int k, int len)
{
memset(num, 0, sizeof(num));
int i, t;
for ( i = 0; i < len; i++ )
{
t= k*str[i];
if (t <= 9)
{
num[i]+=t;
if (num[i]>9)
{
num[i+1]=num[i]/10;
num[i]%=10;
}
}
else
{
num[i]+=t%10;
if (num[i]>9)
{
num[i+1]=num[i]/10;
num[i]%=10;
}
num[i+1]+=t/10;
}
}
if (num[i+1]) i++;
return i;
}
int cmp(int len1, int len2)
{
int i, j;
if (len2 != len1 ) return 0;
for ( i = 0; i < len1; i++)
{
if (str[i]==num[0])
{
for ( j = 0; (j < len1 ) ; j++ )
if (str[(i+j)%len1] != num[j%len2])
break;
if ( j == len1 ) return 1;
}
}
return 0;
}
int main()
{
while (EOF != scanf("%s", str))
{
printf("%s is ", str);
int len= strlen(str), i, flag=1;
for ( i = 0; i < len; i++ )
{
str[i]-='0';
}
turn(len);
for ( i = 1; i <= len; i++ )
{
int len2= calu(i, len);
if ( !cmp(len, len2) )
{
printf("not cyclic\n");
flag=0;
break;
}
}
if ( flag ) printf("cyclic\n");
}
return 0;
}