您的位置:首页 > 编程语言 > Go语言

POJ 1047  Round and Round We Go

2012-12-05 17:10 483 查看
Round and Round We Go

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 9153Accepted: 4144
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

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

#include<stdio.h>

#include<string.h>

int len;

void mnu(int n,char str1[],char str2[])

{

int i,num[100]={0},a[100],j,k=0,sum=0;

for(i=len-1;i>=0;i--)

a[k++]=str1[i]-'0';

for(i=0;i<len;i++)

{

num[i]+=a[i]*n;

num[i]+=sum;

sum=num[i]/10;

num[i]=num[i];

}

while(sum!=0){

num[i]=sum;

sum=sum/10;

i++;

}

j=0;

for(k=i-1;k>=0;k--)

str2[j++]=num[k]+'0';

str2[j]=0;

}

int range(char str1[],char str2[])

{

char str[150];

strcpy(str,str1);

strcat(str,str1);

if(strstr(str,str2)!=NULL)

return 1;

else return 0;

}

int main()

{

int i;

char str1[65],str2[65];

while(gets(str1))

{

len=strlen(str1);

for(i=2;i<=len;i++)

{

mnu(i,str1,str2);

if(!range(str1,str2))

break;

}

if(i>len)

printf("%s is cyclic\n",str1);

else printf("%s is not cyclic\n",str1);

}

return 0;

}

0000
0000 is cyclic
1111
1111 is not cyclic
142857
142857 is cyclic
0588235294117647
0588235294117647 is cyclic
052631578947368421
052631578947368421 is cyclic
0434782608695652173913
0434782608695652173913 is cyclic
0344827586206896551724137931
0344827586206896551724137931 is cyclic
0212765957446808510638297872340425531914893617
0212765957446808510638297872340425531914893617 is cyclic

0169491525423728813559322033898305084745762711864406779661
0169491525423728813559322033898305084745762711864406779661 is
cyclic

016393442622950819672131147540983606557377049180327868852459
016393442622950819672131147540983606557377049180327868852459
is cyclic
01
01 is not cyclic
758241
758241 is not cyclic
0588235294117646
0588235294117646 is not cyclic
152631578947368421
152631578947368421 is not cyclic
04347826086956521739130
04347826086956521739130 is not cyclic
034482758620689655172413793
034482758620689655172413793 is not cyclic
0212765957446808501638297872340425531914893617
0212765957446808501638297872340425531914893617 is not
cyclic

0169491525423728813559322033898305084745762711864406779666
0169491525423728813559322033898305084745762711864406779666 is
not cyclic

485767305968737584736478947631110239847564719283674623819109
485767305968737584736478947631110239847564719283674623819109
is not cyclic

999999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999
is not cyclic
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: