Page 1 of 6
202 - Repeating Decimals
Posted: Sat Jun 08, 2002 2:03 pm
by nayaya
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]
Posted: Sat Jun 08, 2002 2:09 pm
by xenon
your arrays are too small. the program tries to access elements outside the range [0..100].
Posted: Sat Jun 08, 2002 2:18 pm
by nayaya
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.
Posted: Sat Jun 08, 2002 2:57 pm
by xenon
200 is still too small then... there's no penalty for using large arrays, so why don't try something like 10000?
202 - Repeating Decimals || Sample input/Output plz ...
Posted: Sat Sep 28, 2002 6:12 pm
by laboni
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
Question: abort 202 output format...
Posted: Fri Dec 20, 2002 5:47 pm
by Johnny shu
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...
thx for reply !!!

Re: Question: abort 202 output format...
Posted: Mon Dec 30, 2002 2:49 pm
by PdR
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.
Posted: Wed Jan 01, 2003 5:08 am
by htl
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]
Posted: Fri Jan 03, 2003 6:08 am
by yatsen
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.
202-- Where should place the parenthesis?
Posted: Thu Jan 30, 2003 4:20 am
by Rein
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?
Posted: Thu Jan 30, 2003 5:44 am
by Red Scorpion
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.
Posted: Thu Jan 30, 2003 5:51 am
by Red Scorpion
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.
Posted: Fri Jan 31, 2003 4:22 am
by Red Scorpion
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.
Posted: Fri Jan 31, 2003 4:39 am
by Rein
Um... I found another mistake in my code...and just got AC
Anyway, thank you very much.
Posted: Mon Mar 10, 2003 3:51 pm
by little joey
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'.