<poj1047>Round and Round We Go

Round and Round We Go
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10410   Accepted: 4766

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 

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

Source

大数乘法部分不是难点,本题的难点是怎么判断进行大数乘法之后得到的新数从某个digit开始读就是原数,我参考一个网友提供的方法,假设存储原数的字符数组为ori_str,储存进行大数阶乘之后的数的字符数组是num_str,两个ori_str连接成一个新的字符串exp_str,若原数是cyclic的则num_str是exp_str的sub-string。
判断sub-string可以使用c语言string.h的strstr(http://www.cplusplus.com/reference/clibrary/cstring/strstr/),也可以使用STL的string::find(http://www.cplusplus.com/reference/string/string/find/).本文使用前者。
AC Code:
#include <stdio.h>
#include <string.h>
int main()
{
    int num[60],MulNum[60],i,j,k,len;//num是原数,MulNum是进行乘法之后的数 
    char ori_str[61],num_str[61],exp_str[121];//ori_str是原数,num_str是进行乘法之后的数,exp_str由 
                                              //两个ori_str连接而成                                                       
    int temp,carry;
    int flag;
    while(scanf("%s",ori_str)!=EOF)
    {
        flag=1;
        strcpy(exp_str,ori_str);
        len=strlen(ori_str);
        for(i=0;i<len;i++)
        {
            exp_str[len+i]=ori_str[i];
            num[i]=ori_str[i]-'0';
        }
        exp_str[len+i]='\0';
        for(i=2;i<=len;i++)
        {
            carry=0;
            for(j=len-1;j>-1;j--)
            {
                MulNum[j]=num[j]*i+carry;
                if(MulNum[j]>9)
                {
                    temp=MulNum[j];
                    MulNum[j]%=10;
                    carry=temp/10;
                }
                else carry=0;
            }
            //如果carry不是0,则上面算出来的数位数多于原数,原数不是cyclic
            if(carry)
            {
                flag=0;
                break;
            }
            //判断是否cyclic
            else
            {
                for(j=0;j<len;j++)
                {
                    num_str[j]=MulNum[j]+'0';
                }
                num_str[len]='\0';
                if(strstr(exp_str,num_str)==NULL)
                {
                    flag=0;
                    break;
                }
            }
        }//for
        if(flag) printf("%s is cyclic\n",ori_str);
        else printf("%s is not cyclic\n",ori_str);
    }//while
    return 0;
}


原文地址:https://www.cnblogs.com/cszlg/p/2910505.html