poj 1047 Round and Round We Go

C语言题库

共 2138字,需浏览 5分钟

 · 2021-12-13

Round and Round We Go


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



一轮又一轮我们走


描述

循环数是长度为n位的整数,当乘以从1到n的任何整数时,产生原始数位的“循环”。也就是说,如果您考虑将最后一位数字后面的数字“环绕”回到第一位数字,则两个数字中的数字顺序将相同,尽管它们可能从不同的位置开始。例如,数字 142857 是循环,如下表所示:
142857 *1 = 142857
142857 *2 = 285714
142857 *3 = 428571
142857 *4 = 571428
142857 *5 = 7142857
*4 14

输入

编写一个程序来确定数字是否循环。输入文件是一个长度为 2 到 60 位的整数列表。(请注意,前面的零不应被删除,它们被视为数字的一部分,并在确定 n 时计数。因此,“01”是两位数,与“1”不同,后者是一位数。)

输出

对于每个输入整数,在输出中写一行指示它是否是循环的。

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 
#include
#include
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 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 ans[j] = num[j]*i;
for(k=0;k {
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 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 printf("%c",str[j]);
printf(" is not cyclic\n");
memset(str,0,sizeof(str));
break;
}
}
}
return 0;
}


浏览 27
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报