## 332 - Rational Numbers from Repeating Fractions

Moderator: Board moderators

Axiom
New poster
Posts: 3
Joined: Wed Dec 05, 2001 2:00 am
hi!
does anyone know wat to look out for in the input set for this qn?

Thanx!

Yeamin Rajeev
New poster
Posts: 7
Joined: Wed Dec 12, 2001 2:00 am
Should Try for j = 0. When the denominator is a multiple of 2 and/or 5 only!
``Fire in the Sky - can't you see that all my castles are burning?''

Axiom
New poster
Posts: 3
Joined: Wed Dec 05, 2001 2:00 am
the output for j==0 is 0/1??
why is there a need to check for denominator tat is multiple of 2 and/or 5?

wat i do is find the GCD of the nominator n denominator n keep reducing the fraction with it.

Yeamin Rajeev
New poster
Posts: 7
Joined: Wed Dec 12, 2001 2:00 am
Try this:

input
0 0.35

output
7/20
``Fire in the Sky - can't you see that all my castles are burning?''

jpfarias
Learning poster
Posts: 98
Joined: Thu Nov 01, 2001 2:00 am
Location: Brazil
Contact:

### Getting WA too...

I take care about cases where j == 0, but still got WA.

For:
0 0.5

For:
0 0.35

Can't get what's wrong...
BTW, should I compute the GCD between the denumerator and the numerator more than once? I think that the GCD will reduce it the most possible...

Ivan Golubev
Experienced poster
Posts: 167
Joined: Fri Oct 19, 2001 2:00 am
Location: Saint Petersburg, Russia
Try your solution with this I/O (AFAIR, it's from old message board).

Input:
2 0.318
1 0.3
2 0.09
6 0.714285
6 0.714285000
1 0.9999
9 0.123456789
8 0.987654321
9 0.574454131
5 0.83777471
1 0.22222222
9 0.111111111
9 0.222222222
9 0.333333333
9 0.444444444
9 0.555555555
9 0.666666666
9 0.777777777
9 0.888888888
9 0.999999999
9 0.000000000
1 0.200
8 0.200000000
8 0.020000000
0 0.3
0 0.5
0 0.55
1 0.55
6 0.142857
0 0.9
1 0.9
6 0.076923
0 0.678453453
0 0.1
2 0.31818
9 0.345678993
2 0.25
1 0.3
0 0.3
0 0.0
5 0.99999
6 0.714285
0 0.5
0 0.35
-1

Output:
Case 1: 7/22
Case 2: 1/3
Case 3: 1/11
Case 4: 5/7
Case 5: 119047381/166666500
Case 6: 1/1
Case 7: 13717421/111111111
Case 8: 54869684/55555555
Case 9: 574454131/999999999
Case 10: 41888317/49999500
Case 11: 2/9
Case 12: 1/9
Case 13: 2/9
Case 14: 1/3
Case 15: 4/9
Case 16: 5/9
Case 17: 2/3
Case 18: 7/9
Case 19: 8/9
Case 20: 1/1
Case 21: 0/1
Case 22: 1/5
Case 23: 1/5
Case 24: 2000000/99999999
Case 25: 3/10
Case 26: 1/2
Case 27: 11/20
Case 28: 5/9
Case 29: 1/7
Case 30: 9/10
Case 31: 1/1
Case 32: 1/13
Case 33: 678453453/1000000000
Case 34: 1/10
Case 35: 7/22
Case 36: 38408777/111111111
Case 37: 25/99
Case 38: 1/3
Case 39: 3/10
Case 40: 0/1
Case 41: 1/1
Case 42: 5/7
Case 43: 1/2
Case 44: 7/20

jpfarias
Learning poster
Posts: 98
Joined: Thu Nov 01, 2001 2:00 am
Location: Brazil
Contact:

### Got PE

Thank u a lot!!!

The problem with my one was that I was using unsigned long, so I've changed it to unsigned long long and got PE. I'll see what's still wrong in my output later...

Thanx..............

Jo

broderic
New poster
Posts: 34
Joined: Thu Jun 06, 2002 4:35 am
Could anybody help me out? my output matches the above tests
exactly, but I still get WA. any ideas?
Here's me code, I'll delete it if anybody helps me out.
Last edited by broderic on Tue Aug 13, 2002 6:47 am, edited 1 time in total.

ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

### a few suggestions

1. try not to use double.

2. when you output, no need to output the numbers as 11 characters.

broderic
New poster
Posts: 34
Joined: Thu Jun 06, 2002 4:35 am
hehe, thanks for the tip. I'm not sure why i was even using
doubles to begin with, the integer-only solution is pretty obvious
and easy to code.

Oh, btw, those were L's (not ones) for printing long long's.

Broderick

Shantanu
New poster
Posts: 8
Joined: Sun Oct 20, 2002 6:44 am

### Fix my problem

Can anyone tell me where is my fault:
#include <stdio.h>

#include <math.h>

double gcd(double m,double n)
{
double r=fmod(m,n);
while(r)
{
m=n;
n=r;
r=fmod(m,n);
}
return n;
}

void main(void)
{

double cse=1,x,nn,dd,j,k,div,pp,d,f;
unsigned long long dummy;
double dum;

while(2==scanf("%lf%lf",&j,&x) && j!=-1)
{
if(x==0)
{
printf("Case %0.lf: 0/1\n",cse++);
continue;
}
f=1;
nn=x*pow(10,j);
for(k=0;;k++)
{
dummy=nn;
dum=dummy;
if(dum==nn)break;

nn*=10;
f*=10;
}
if(j)
{
modf((x*pow(10,k)),&d);
nn-=d;
dd=pow(10,k+j)-pow(10,k);
}
else dd=f;

div=gcd(dd,nn);
printf("Case %0.lf: %.0lf/%.0lf\n",cse++,nn/div,dd/div);
}
}

Ming Han
Learning poster
Posts: 77
Joined: Thu Jun 06, 2002 7:10 pm
Location: Singapore
Contact:

### ??

Can you please tell us what question is this?

And what do you want?
:: HanWorks ::

Shantanu
New poster
Posts: 8
Joined: Sun Oct 20, 2002 6:44 am
Sorry, the number of the problem 332

supermin
New poster
Posts: 37
Joined: Sat Oct 12, 2002 9:54 am
Location: (Taiwan)
Contact:

Could anyone tell me why I got P.E.

Is there any wrong about the presentation?

I see only 18 Accepted and many (P.E.)s

Jalal
Learning poster
Posts: 65
Joined: Sun Jun 02, 2002 8:41 pm