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

nayaya
New poster
Posts: 9
Joined: Sat Jun 08, 2002 1:57 pm

202 - Repeating Decimals

Post by nayaya » Sat Jun 08, 2002 2:03 pm

Why do I always get a Runtime Error? Always Invalid memory reference?
What' wrong with my pascal program?

[pascal]
program p202(input,output)
type
sss=array[0..100] of integer;
var
n,d,k,i,m:integer;
left,num:sss;
tf:boolean;

procedure work;
begin
num[0]:=n div d;
left[0]:=n mod d;
k:=0;tf:=true;
while (left[k]<>0) and (tf) do
begin
k:=k+1;
num[k]:=left[k-1]*10 div d;
left[k]:=left[k-1]*10 mod d;
if left[k]<>0 then for i:=1 to k-1 do
if (left=left[k]) and (num=num[k]) then
begin
tf:=false;m:=i;
end;
end;
write(n,'/',d,' = ',num[0]);
if k<>0 then
begin
write('.');
if tf then
begin
for i:=1 to k do write(num);
m:=k-1;
write('(0)');
end
else
begin
for i:=1 to m-1 do write(num);
write('(');
if (k-m>50) then
begin
for i:=m to m+49 do write(num);
write('...');
end
else
for i:=m to k-1 do write(num);
write(')');
end
end;
writeln;
writeln(' ',k-m,' = number of digits in the repeating circle');
writeln;
end;

Begin
while not eof(input) do
begin
readln(n,d);
work;
end;
end.

[/pascal]

xenon
Learning poster
Posts: 100
Joined: Fri May 24, 2002 10:35 am
Location: Scheveningen, Holland

Post by xenon » Sat Jun 08, 2002 2:09 pm

your arrays are too small. the program tries to access elements outside the range [0..100].

nayaya
New poster
Posts: 9
Joined: Sat Jun 08, 2002 1:57 pm

Post by nayaya » Sat Jun 08, 2002 2:18 pm

xenon wrote:your arrays are too small. the program tries to access elements outside the range [0..100].
Thank you. But I tried [0..200] and nothing changed, still Invalid memory reference.

xenon
Learning poster
Posts: 100
Joined: Fri May 24, 2002 10:35 am
Location: Scheveningen, Holland

Post by xenon » Sat Jun 08, 2002 2:57 pm

200 is still too small then... there's no penalty for using large arrays, so why don't try something like 10000?

laboni
New poster
Posts: 12
Joined: Sat Sep 14, 2002 9:22 pm
Location: India

202 - Repeating Decimals || Sample input/Output plz ...

Post by laboni » Sat Sep 28, 2002 6:12 pm

Hi!

Can anyone provide me some critical input/output for this problem.
I tested with different types of inputs and found my program working.
But OJ says WA. May be your iput will help me to fix the error.
Thanks for reading my post.

Bye

Johnny shu
New poster
Posts: 1
Joined: Fri Dec 20, 2002 5:37 pm

Question: abort 202 output format...

Post by Johnny shu » Fri Dec 20, 2002 5:47 pm

if numerator are 0 output should be " 0.0(0)" or "0(0)" or another??

and there are still some left should be pay attention?
my answer always wrong... :oops:

thx for reply !!! :P

PdR
New poster
Posts: 24
Joined: Mon Dec 30, 2002 4:27 am

Re: Question: abort 202 output format...

Post by PdR » Mon Dec 30, 2002 2:49 pm

0/9 = 0.(0)
1 = number of digits in repeating cycle

123/231 = 0.(532467)
6 = number of digits in repeating cycle

1234/2314 = 0.(53327571305099394987035436473638720829732065687121...)
132 = number of digits in repeating cycle
These three examples should do it.

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

Post by htl » Wed Jan 01, 2003 5:08 am

My prog alway got WA. Do I misunderstand the meaning of the prob?
[c]
#include<stdio.h>
#define YES 1
#define NO 0
int gcd(int,int);
void main(void)
{
int a,b,x,digit[4000],temp[4000],count,found,len,y,z;
while(scanf("%d %d",&a,&b)!=EOF)
{
printf("%d/%d = %d.",a,b,a/b);
if(a==0)
{
printf("(0)\n 1 = number of digits in repeating cycle\n");
continue;
}
if(a>=b)
x=gcd(a,b);
else
x=gcd(b,a);
a/=x,b/=x,a%=b;
for(count=0,found=NO;!found;)
{
a*=10;
digit[count]=a/b,temp[count++]=a%b;
if(a%b==0)
break;
a%=b;
for(x=0;x<count-1;x++)
if(digit[x]==digit[count-1] && temp[x]==temp[count-1])
{
found=YES;
break;
}
}
z=0;
if(found)
{
len=count-1-x;
for(y=0;y<x;y++)
{
printf("%d",digit[y]),z++;
if(z==50)
break;
}
if(z<50)
{
printf("(");
for(y=x;y<count-1;y++)
{
if(z==50)
break;
printf("%d",digit[y]),z++;
}
if(y<count-1)
printf("...");
printf(")\n");
}
printf(" %d = number of digits in repeating cycle\n",len);
}
else
{
len=count;
for(x=0;x<count;x++)
printf("%d",digit[x]);
printf("(0)\n 1 = number of digits in repeating cycle\n");
}
}
}
int gcd(int a,int b)
{
int r;
r=1;
while(r>0)
{
r=a%b;
a=b;
b=r;
}
return a;
}
[/c]

yatsen
Learning poster
Posts: 68
Joined: Fri Nov 23, 2001 2:00 am
Location: taiwan

Post by yatsen » Fri Jan 03, 2003 6:08 am

htl wrote:My prog alway got WA. Do I misunderstand the meaning of the prob?
Try this input: 1 1

Your output is:
1/1 = 1.0(0)
1 = number of digits in repeating cycle

The correct answer is:
1/1 = 1.(0)
1 = number of digits in repeating cycle

p.s. You do not output a blank line after each test case, although it just causes PE.

Rein
New poster
Posts: 5
Joined: Sun Jul 28, 2002 11:39 am

202-- Where should place the parenthesis?

Post by Rein » Thu Jan 30, 2003 4:20 am

It said:
In writing the decimal expansion, enclose the repeating cycle in parentheses when possible. If the entire repeating cycle does not occur within the first 50 places, place a left parenthesis where the cycle begins - it will begin within the first 50 places - and place ``...)" after the 50th digit.
But what if there is a expasion like this:

Code: Select all

0.[12989874746764.....](.....7696)
and the digits in [] is more than 50,
where does the "first place" begins?
And should I place the left parenthesis?

Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am

Post by Red Scorpion » Thu Jan 30, 2003 5:44 am

Try this test cases :

Input:
78 1009
3000 9992
0 1
3 4
9 2
18 9
229 3000

Output :
8/1009 = 0.(07730426164519326065411298315163528245787908820614...)
252 = number of digits in repeating cycle

3000/9992 = 0.(30024019215372297838270616493194555644515612489991...)
208 = number of digits in repeating cycle

0/1 = 0.(0)
1 = number of digits in repeating cycle

3/4 = 0.75(0)
1 = number of digits in repeating cycle

9/2 = 4.5(0)
1 = number of digits in repeating cycle

18/9 = 2.(0)
1 = number of digits in repeating cycle

229/3000 = 0.076(3)
1 = number of digits in repeating cycle

/*Remember the above output is not the problem request*//
I hope this help.

Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am

Post by Red Scorpion » Thu Jan 30, 2003 5:51 am

Try this test cases :

Input:
78 1009
3000 9992
0 1
3 4
9 2
18 9
229 3000

Output :
8/1009 = 0.(07730426164519326065411298315163528245787908820614...)
252 = number of digits in repeating cycle

3000/9992 = 0.(30024019215372297838270616493194555644515612489991...)
208 = number of digits in repeating cycle

0/1 = 0.(0)
1 = number of digits in repeating cycle

3/4 = 0.75(0)
1 = number of digits in repeating cycle

9/2 = 4.5(0)
1 = number of digits in repeating cycle

18/9 = 2.(0)
1 = number of digits in repeating cycle

229/3000 = 0.076(3)
1 = number of digits in repeating cycle

/*Remember the above output is not the problem request*//
I hope this help.

Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am

Post by Red Scorpion » Fri Jan 31, 2003 4:22 am

Try This test Case :
Input :

78 1009
3000 9992
0 1
3 4
9 2
18 9
229 3000

Output :

78/1009 = 0.(07730426164519326065411298315163528245787908820614...)
252 = number of digits in repeating cycle

3000/9992 = 0.(30024019215372297838270616493194555644515612489991...)
208 = number of digits in repeating cycle

0/1 = 0.(0)
1 = number of digits in repeating cycle

3/4 = 0.75(0)
1 = number of digits in repeating cycle

9/2 = 4.5(0)
1 = number of digits in repeating cycle

18/9 = 2.(0)
1 = number of digits in repeating cycle

229/3000 = 0.076(3)
1 = number of digits in repeating cycle

Good Luck.

Rein
New poster
Posts: 5
Joined: Sun Jul 28, 2002 11:39 am

Post by Rein » Fri Jan 31, 2003 4:39 am

Um... I found another mistake in my code...and just got AC

Anyway, thank you very much.

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Mon Mar 10, 2003 3:51 pm

Hmm... Red Scorpion, my program went bunkers over your input...
It says that none of the integers exceeds 3000, so 3000/9992 is incorrect.

I've been struggeling with this one, but finaly got AC. One word of warning: 'Look out for empty lines'.

Post Reply

Return to “Volume 2 (200-299)”