POJ 1047

简介: Round and Round We Go Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10514   Accepted: 4812 Description A cyclic n...
Round and Round We Go
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10514   Accepted: 4812

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




#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int N = 65;
char str[N];
int num[N],ans[N],len;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
bool is_match()
{
    qsort(num,len,sizeof(int),cmp);
    qsort(ans,len,sizeof(int),cmp);
    int i;
    for(i=0;i<len;i++)
        if(num[i]!=ans[i])
            return false;
    return true;
}        
int main()
{
    int i,j,k;
    memset(num,0,sizeof(num));
    memset(str,0,sizeof(str));
    while(~scanf("%s",str))
    {
        len=strlen(str);
        j=0;
        memset(num,0,sizeof(num));
        for(i=len-1;i>=0;i--) 
            num[j++]=str[i]-'0';
        for(i=2;i<=len;i++)
        {
            memset(ans,0,sizeof(ans));
            for(j=0;j<len;j++)
            {         
                ans[j] = num[j]*i;
            }
            /*
            for(k=0;k<len;k++)
                printf("%d ",ans[k]);
            putchar('\n');
            */
            for(k=0;k<len;k++)//虽然最多增加2位 , 但匹配函数是默认长度相同的 
            {
                if(ans[k]>9)
                {
                    ans[k+1] += ans[k]/10;//两条语句不可交换,搞了好久才发现 
                    ans[k] %= 10;
                }
            }
            bool flag = is_match();
            if(flag)
            {
                if(i==len)
                {
                    for(j=0;j<len;j++)
                        printf("%c",str[j]);
                    printf(" is cyclic\n");
                    memset(str,0,sizeof(str)); 
                }
                k=0;
                for(j=len-1;j>=0;j--) 
                    num[k++]=str[j]-'0';
            }
            else
            {
                 for(j=0;j<len;j++)
                    printf("%c",str[j]);
                 printf(" is not cyclic\n");
                 memset(str,0,sizeof(str)); 
                 break;
            }                   
        }      
    }
    return 0;
}                          


 
对于数N,若N为循环的则有N*(length(N)+1)=99....99, (length(N)个9),length(N)为N的位数,含前导0。应该还是简单的...   
//此为转载
#include<iostream>
#include<string>
using namespace std;
bool fun(string str)
{
    int n=str.length()+1;
    int i,up=0,temp=0;
    for (i=n-2;i>=0;i--)
    {
        temp=( int )(str[i]- '0' );
        if ((temp*n+up)%10!=9) return false ;
        up=(temp*n+up)/10;
    }
    return true ;
}
int main()
{
    string str1;
    while (cin>>str1)
    {
        if (fun(str1))
        {
            cout<<str1<< " is cyclic" <<endl;
        }
        else
        {
            cout<<str1<< " is not cyclic" <<endl;
        }
    }
    return 0;
}

  







目录
相关文章
|
7月前
|
算法
Wormholes—POJ3259
Wormholes—POJ3259
POJ 2027 No Brainer
POJ 2027 No Brainer
113 0
POJ 1804
题目:http://poj.org/problem?id=1804 大意:给你一串数字,排序。求出最少的交换次数  \ 我用归并做的 #include #include using namespace std; int aa[500010],bb[500010]; long lon...
702 0
POJ 2487 Stamps
Description Background Everybody hates Raymond. He’s the largest stamp collector on planet earth and because of that he always makes fun of all the others at the stamp collector parties.
1067 0
|
人工智能
POJ 1936 All in All
Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way.
796 0
|
机器学习/深度学习 算法
|
算法 机器人 编译器
POJ-2632
#include int main() { int k,a,b,n,m,i,j,num,rep,rect[100][100],robot[100][3]; int flag; char c; for(scanf("%d...
932 0
|
机器学习/深度学习
|
机器学习/深度学习
|
机器学习/深度学习