Round and Round We Go
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8338 | Accepted: 3770 |
Description
A cyclic number is an integer n digits in length which, when multiplied by any integer from 1 to n, yields a"cycle"of the digits of the original number. That is, if you consider the number after the last digit to "wrap around"back to the first digit, the sequence of digits in both numbers will be the same, though they may start at different positions.For example, the number 142857 is cyclic, as illustrated by the following table:
142857 *1 = 142857
142857 *2 = 285714
142857 *3 = 428571
142857 *4 = 571428
142857 *5 = 714285
142857 *6 = 857142
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 not numbers are cyclic. The input file is a list of integers from 2 to 60 digits in length. (Note that preceding zeros should not be removed, they are considered part of the number and count in determining n. Thus, "01"is a two-digit number, distinct from "1" which is a one-digit number.)
Output
For each input integer, write a line in the output indicating 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
</pre><pre code_snippet_id="1628004" snippet_file_name="blog_20160329_4_3264100" class="sio" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">大数乘法:
<pre name="code" class="cpp">#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 64 char AllCircleNumbers[N][N]; int n; void getAllCircleNumbers(char num[]) { strcpy(AllCircleNumbers[0],num); for(int i=1;i<n;i++) { int k=0; for(int j=i;j<n;j++) AllCircleNumbers[i][k++]=num[j]; for(int j=0;j<i;j++) AllCircleNumbers[i][k++]=num[j]; AllCircleNumbers[i][k]=0; } } int isSameCircle(char inum[]) { for(int i=0;i<n;i++) if(strcmp(AllCircleNumbers[i],inum)==0) return 1; return 0; } char* nMultiplyNumber(char num[], int i) { char* inum =(char*)malloc(sizeof(char)*N); int a[N]; for(int k=n-1;k>=0;k--) a[k]=(num[k]-'0')*i; for(int k=n-1;k>=1;k--) { a[k-1]+=(a[k]/10); a[k]%=10; } a[0]=a[0]%10; for(int k=n-1;k>=0;k--) inum[k]=a[k]+'0'; inum[n]=0; return inum; } int isCircleNumber(char num[]) { n=strlen(num); getAllCircleNumbers(num); for(int i=2;i<=n;i++) { char* inum=nMultiplyNumber(num,i); if(!isSameCircle(inum)) { free(inum); return 0; } else { free(inum); } } return 1; } int main(int argc, char* argv[]) { int k; char num[128]; while(scanf("%s",num)!=EOF) { if(isCircleNumber(num)) printf("%s is cyclic\n"); else printf("%s is not cyclic\n"); } return 0; }
当然,数学好的会有更好地方法http://blog.youkuaiyun.com/rongyongfeikai2/article/details/6043514