poj 1047 Round And Round We Go

来源:互联网 发布:h5 手机页面模版源码 编辑:程序博客网 时间:2024/05/23 01:16

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

Greater New York 2001

这题要用大整数乘法,但是却不是大整数和大整数相乘,这样太浪费时间了,用大整数和小正数相乘,这样可以节约不少时间,另外,比较的话,也不用一个一个的比较,本身复制一遍,直接连上原串就可以了。

/*Problem: 1047       User: sarukaMemory: 340K        Time: 0MSLanguage: G++       Result: Accepted*/#include<cstdio>#include<cstring>const int maxn = 65;char s[maxn];int num[maxn], temp[maxn];int flag, len;int mult(int *num, int *temp, int n) //num中的数与n相乘存到temp中,如果结果位数大于num则返回0,否则返回1{    int t = 0;    for(int i = 0; i < len; i++)    {        t = num[i] * n + t;        temp[i] = t % 10;        t /= 10;    }    if(t) return 0;    return 1;}int Judge(int *num, int *temp)      //判断是否匹配{    int j, k;    for(int i = 0; i < len; i++)    {        k = 0;        if(temp[i] == num[0])        {            j = i;            while(k < len && num[++k] == temp[(++j) % len]);            if(k == len) return 1;   //说明可以匹配        }    }    return 0;}int main(){    while(gets(s))    {        flag = 1;        len = strlen(s);        memset(num, 0, sizeof(num));        for(int i = len - 1, j = 0; i >= 0; i--)        {            num[j++] = s[i] - '0';        }         for(int i = 2; i <= len; i++)        {            memset(temp, 0, sizeof(temp));            if(mult(num, temp, i))                {                if(Judge(num, temp) == 0)                {                    printf("%s is not cyclic\n", s);                    flag = 0;                    break;                }            }            else //相乘的结果位数增多肯定不满足,直接跳出            {                printf("%s is not cyclic\n", s);                flag = 0;                break;            }        }        if(flag)            printf("%s is cyclic\n",s);    }    return 0;}

Powered By Saruka.
Copyright © 2016 All Rights Reserved.

0 0