Posted: Mon Feb 07, 2005 3:11 pm
Hey, that worked, thanks. But I cannot believe there are test cases where the result is so long...
Code: Select all
0 5
76 25
5 43
1 397
0 101
1 101
20 5
33 70
3 77
15 899
3 7
0 9
1 999
15 89
12 2999
12 1000
2999 3000
Code: Select all
0/5 = 0.(0)
1 = number of digits in repeating cycle
76/25 = 3.04(0)
1 = number of digits in repeating cycle
5/43 = 0.(116279069767441860465)
21 = number of digits in repeating cycle
1/397 = 0.(00251889168765743073047858942065491183879093198992...)
99 = number of digits in repeating cycle
0/101 = 0.(0)
1 = number of digits in repeating cycle
1/101 = 0.(0099)
4 = number of digits in repeating cycle
20/5 = 4.(0)
1 = number of digits in repeating cycle
33/70 = 0.4(714285)
6 = number of digits in repeating cycle
3/77 = 0.(038961)
6 = number of digits in repeating cycle
15/899 = 0.(01668520578420467185761957730812013348164627363737...)
420 = number of digits in repeating cycle
3/7 = 0.(428571)
6 = number of digits in repeating cycle
0/9 = 0.(0)
1 = number of digits in repeating cycle
1/999 = 0.(001)
3 = number of digits in repeating cycle
15/89 = 0.(16853932584269662921348314606741573033707865)
44 = number of digits in repeating cycle
12/2999 = 0.(00400133377792597532510836945648549516505501833944...)
1499 = number of digits in repeating cycle
12/1000 = 0.012(0)
1 = number of digits in repeating cycle
2999/3000 = 0.999(6)
1 = number of digits in repeating cycle
Code: Select all
2772 883
22 7
678 119
1011 2123
1 303
1 300
Code: Select all
2772/883 = 3.(13929784824462061155152887882219705549263873159682...)
441 = number of digits in repeating cycle
22/7 = 3.(142857)
6 = number of digits in repeating cycle
678/119 = 5.(697478991596638655462184873949579831932773109243)
48 = number of digits in repeating cycle
1011/2123 = 0.(47621290626471973622232689590202543570419218087611...)
192 = number of digits in repeating cycle
1/303 = 0.(0033)
4 = number of digits in repeating cycle
1/300 = 0.00(3)
1 = number of digits in repeating cycle
Code: Select all
0 3000
1 1
9 9
3000 2999
2999 2997
Code: Select all
0/3000 = 0.(0)
1 = number of digits in repeating cycle
1/1 = 1.(0)
1 = number of digits in repeating cycle
9/9 = 1.(0)
1 = number of digits in repeating cycle
3000/2999 = 1.(00033344448149383127709236412137379126375458486162...)
1499 = number of digits in repeating cycle
2999/2997 = 1.(000667334)
9 = number of digits in repeating cycle
Code: Select all
#include <iostream>
#include <stdio.h>
using namespace std;
#define max 30010
int num[max+1], n;
char digit[100000000];
int main (void)
{
int numerator, nominator;
int i, j, st, en;
while(scanf("%d%d",&numerator,&nominator)==2){
printf("%d%c%d = %d.",numerator,'/',nominator,numerator/nominator);
n=(numerator%nominator);
for(i=0;i<=nominator*10;i++)num[i]=0;
i=1;
while(1){
if(num[n]){
st=num[n];
en=i;
if(n<nominator && num[n*10])st=num[n*10];
break;
}
if(!n){
digit[i]='0';
i++;
st=i-1;
en=i;
break;
}
num[n]=i;
if(n<nominator)n*=10;
num[n]=i;
while(n<nominator){
n*=10;
digit[i]='0';
i++;
num[n]=i;
}
digit[i]=(n/nominator)+'0';
n=n%nominator;
i++;
}
for(i=1;i<st;i++)cout<<digit[i];
cout<<"(";
for(i=st;i<en;i++){
if((i-st)==50){
cout<<"...";
break;
}
cout<<digit[i];
}
cout<<")"<<endl;
printf(" %d = number of digits in repeating cycle\n\n",en-st);
//digit[i+1]='\0';
//cout<<digit<<endl;
}
return 0;
}
Code: Select all
1 397
10 397
100 397
1000 397
1 251
1 753
15 1255
Code: Select all
1/397 = 0.(00251889168765743073047858942065491183879093198992...)
99 = number of digits in repeating cycle
10/397 = 0.(02518891687657430730478589420654911838790931989924...)
99 = number of digits in repeating cycle
100/397 = 0.(25188916876574307304785894206549118387909319899244...)
99 = number of digits in repeating cycle
1000/397 = 2.(51889168765743073047858942065491183879093198992443...)
99 = number of digits in repeating cycle
1/251 = 0.(00398406374501992031872509960159362549800796812749...)
50 = number of digits in repeating cycle
1/753 = 0.(00132802124833997343957503320053120849933598937583...)
50 = number of digits in repeating cycle
15/1255 = 0.(01195219123505976095617529880478087649402390438247...)
50 = number of digits in repeating cycle
Code: Select all
#include<stdio.h>
#include<string.h>
#define YES 1
#define NO 0
int gcd(int a,int b)
{
int c;
for(;a%b;c=b,b=a%b,a=c);
return b;
}
void main()
{
int x,y,found,z,ans,digit[3001],temp[3001],a,b;
/*freopen("d:\\out202.txt","w",stdout);*/
while(scanf("%d %d",&a,&b)==2 && a+b)
{
memset(digit,0,sizeof(digit));
memset(temp,0,sizeof(temp));
printf("%d/%d = %d",a,b,a/b);
x=gcd(a,b);
a/=x,b/=x;
a=a%b;
for(x=0,found=NO;x<3001;x++)
{
a*=10;
digit[x]=a/b;
temp[x]=a%b;
if(a%b==0)break;
for(y=0;y<x;y++)
if(temp[y]==temp[x])
{
found=YES;
break;
}
if(found)break;
a=a%b;
}
printf(".");
ans=x-y;
if(!y && digit[x]==digit[y] && found)x--;
if(!found)
{
if(x)x++;
for(y=0,z=2;y<x;y++,z++)
{
printf("%d",digit[y]);
}
printf("(%d)\n",0);
printf(" %d = number of digits in repeating cycle\n\n",1);
}
else
{
for(a=0;a<50;a++)
{
if(a==x-ans+1)printf("(");
if(a<=x)printf("%d",digit[a]);
else break;
}
if(x>=49)printf("...");
printf(")\n");
printf(" %d = number of digits in repeating cycle\n\n",ans);
}
}
}
Code: Select all
1 2
Code: Select all
1/2 = 0.5(0)
1 = number of digits in repeating cycle
Code: Select all
if(x>=50)printf("...");//not-> if(x>=49)printf("...");