202 - Repeating Decimals

All about problems in Volume 2. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Iwashere
New poster
Posts: 20
Joined: Mon Aug 11, 2003 1:50 pm
Location: Singapore

Post by Iwashere »

Hey, that worked, thanks. But I cannot believe there are test cases where the result is so long...
johnnydog33
New poster
Posts: 10
Joined: Mon Mar 14, 2005 7:35 am

202 Help ME!!!

Post by johnnydog33 »

I had tried all I could did, but IT SITLL WA!
Please help me! Thanks for you reply
:cry:

Delete after AC

Here thanks again
Last edited by johnnydog33 on Thu May 05, 2005 11:06 am, edited 1 time in total.
Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria

Post by Sedefcho »

It's normally a bad idea to just directly post your source
code and say "help ! " :) This is just for your information.
I mean you really attract more the attention of the other people
here, if you first describe with plain words what exactly
your problem is, what your algorithm you use and so on.

So ... What problems exactly do you have with problem 202 ?
Do you have some I/O from your program? Are
you sure you cover all test cases which you have
probably found in other threads dedicated to problem 202 ?

Here is some sample I/O for you ( and for anyone else who
might be interested ) from an ACC program.

Let me know if your program covers them all ( if it prints correct
answers for all the test cases given below ). This would mean
you really have some more specific problem / bug.

Regards!


INPUT

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



OUTPUT

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
johnnydog33
New poster
Posts: 10
Joined: Mon Mar 14, 2005 7:35 am

Post by johnnydog33 »

I'm sorry that I don't know how to ask for help.I 'm new here.I'll pay attention on it.

Could tell me how to find some "small" errors? this kind of errors make our program WA but I can hardly find it. It make me angry and sad. :evil: :cry:

As this problem. I still WA......

Thank for youe help.
johnnydog33
New poster
Posts: 10
Joined: Mon Mar 14, 2005 7:35 am

Post by johnnydog33 »

What a bad error! I add a eoln and got AC
The Input/Output format is really a serious problem for me...
Thanks of you reply
logan
New poster
Posts: 6
Joined: Wed May 25, 2005 10:04 am

202 - Repeating Decimals

Post by logan »

I check the test from other thread but it doesn't help.
I need more tests.
Can anybody help me?

Thank you in anticipation :-)
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

You should try these inputs,

Input:

Code: Select all

2772 883
22 7
678 119
1011 2123
1 303
1 300
Output:

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
Hope it works. :D
Ami ekhono shopno dekhi...
HomePage
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind »

my solution passes for all of this i/o
i cant realize my mistake
i need more critical i/o
plz help me
(i can send my code - if needed)
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Ok then try the following inputs...

Input:

Code: Select all

0 3000
1 1
9 9
3000 2999
2999 2997
And the output is...

Output:

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
Hope it helps :D.
Ami ekhono shopno dekhi...
HomePage
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind »

my program passes this i/o also
here is my code

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;
}
what can i do now?
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Check the following input and output carefully...

Input:

Code: Select all

1 397
10 397
100 397
1000 397
1 251
1 753
15 1255
Output:

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
But your code returns wrong answer.

And another thing. If the denominator is n there can me maximum n digits in repeating cycle... If the max denominator is 3000 there can be maximum 3000 digits in repeating cycle. So, you need an array like num[30001]. And why u are using the array digit[100000000], I don't understand. :)

Remember that if there are more than or exactly 50 digits in repeating cycle then you should print '...' otherwise not.

Best of Luck.
Ami ekhono shopno dekhi...
HomePage
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind »

thank you very much
at last I got ACCEPTED
thanks again
chops
New poster
Posts: 9
Joined: Sat Jan 29, 2005 10:48 pm
Location: dhaka
Contact:

Post by chops »

hi

i passed all the test cases found in the board.but WA.
please help.

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);
		}

	}

}
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Your code is almost ok.....

Just try this input. Your program returns wrong...

Input:

Code: Select all

1 2
Output:

Code: Select all

1/2 = 0.5(0)
   1 = number of digits in repeating cycle
And another thing...

Code: Select all

if(x>=50)printf("...");//not-> if(x>=49)printf("...");
Though I think that there is no '49 repeating cycles' in range 0-3000.
But better to use '>=50'.

Hope it helps.
Ami ekhono shopno dekhi...
HomePage
chops
New poster
Posts: 9
Joined: Sat Jan 29, 2005 10:48 pm
Location: dhaka
Contact:

Post by chops »

Thanks a lot that was the only problem.
Got accepted. :D
Post Reply

Return to “Volume 2 (200-299)”