E - Round and Round We Go
Subject
A cyclic number is an integer n digits in length which,when multiplied by any integer from 1 to n, yields a"cycle"of thedigits of the original number. That is, if you consider the number after thelast digit to "wrap around"back to the first
digit, the sequence ofdigits in both numbers will be the same, though they may start at differentpositions.For example, the number 142857 is cyclic, as illustrated by thefollowing table:
142857 *1 = 142857
142857 *2 = 285714
142857 *3 = 428571
142857 *4 = 571428
142857 *5 = 714285
142857 *6 = 857142
------------------------------------------------------------------------------------------------------------------------------------
Input
Write a program which will determine whether or notnumbers are cyclic. The input file is a list of integers from 2 to 60 digits inlength. (Note that preceding zeros should not be removed, they are consideredpart of the number and count in determining n. Thus, "01"is atwo-digit number, distinct from "1" which is a one-digit number.)
------------------------------------------------------------------------------------------------------------------------------------
Output
For each input integer, write a line in the outputindicating whether or not it is cyclic.
------------------------------------------------------------------------------------------------------------------------------------
Sample Input
142857
142856
142858
01
0588235294117647
------------------------------------------------------------------------------------------------------------------------------------
Sample Output
142857 is cyclic
142856 is not cyclic
142858 is not cyclic
01 is not cyclic
0588235294117647 is cyclic
------------------------------------------------------------------------------------------------------------------------------------
注意一下,乘积的第一位数,是不是大于10,就可以了。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int main() { char a[100]; int b[100]; int num[100],num1[100]; int i,j,len,k,r; while(gets(a)!=NULL) { memset(num1,0,sizeof(num1)); len =strlen(a); for(int i=0;i<len;i++) { num1[a[i]-'0']++; } for( i=0;i<len;i++) { memset(num,0,sizeof(num)); //赋值 for( k=0;k<len;k++) b[k] = a[k]-'0'; //乘积 for( j=0;j<len;j++) b[j] = b[j]*(i+1); //改变 for( j=len-1;j>0;j--) { if(b[j]>=10){ b[j-1]+=b[j]/10; b[j]=b[j]%10; } } //比较 if(b[0]>=10){ num[b[0]/10]++; num[b[0]%10]++; j=1; }else{ j=0; } for( ;j<len;j++) { num[b[j]]++; } for(j=0;j<10;j++) { if(num1[j]!=num[j]){ break; } } if(j!=10){ printf("%s is not cyclic\n",a); break; } } if(i>=len){ printf("%s is cyclic\n",a); } } return 0; }------------------------------------------------------------------------------------------------------------------------------------