Round and Round We Go
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 10716 | Accepted: 4913 |
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
post code:原字符串赋值两次
然后 在每次生成的新的字符串中进行查找,如果木有找到 就跳出 返回失败
首先先 将
#include<stdio.h>
#include<string.h>
char temp[100];
char sample[100];
char ch[200];
int num[100],len;
int add() //add 使字符串相加
{
int i,carry=0;
for(i=len-1;i>=0;i--)
{
temp[i]+=num[i]+carry;
if(temp[i]>'9'){
carry=1;
temp[i]-=10;
}
else carry=0;
}
return carry;
}
int main()
{
int i,judge;
char *pos;
while(scanf("%s",temp)!=EOF)
{
len=strlen(temp);
strcpy(ch,temp); // ch 进行判断 将原字符串链接到后面
strcat(ch,temp);
strcpy(sample,temp); //sample 保存原字符
for(i=0;i<len;i++)
{
num[i]=temp[i]-'0';
}
for(i=1;i<len;i++)
{
judge=add(); //进行叠加 *1就可以忽略了 一定可以
if(judge==1)break;
else {
pos=NULL;
pos=strstr(ch,temp); //先查找 如果找到就可以了
if(pos==NULL)break;
}
}
if(i==len)printf("%s is cyclic\n",sample); //进行了n次 每次都能找到就可以了
else printf("%s is not cyclic\n",sample); //木有找到
}
}
本文介绍了一个用于检测循环数字的算法实现,包括输入验证、循环检测和输出判断。通过字符串操作和数学逻辑,该算法能够高效地识别指定长度内的循环数字。

被折叠的 条评论
为什么被折叠?



