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]
202 - Repeating Decimals
Moderator: Board moderators
202 - Repeating Decimals
202 - Repeating Decimals || Sample input/Output plz ...
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
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
-
- New poster
- Posts: 1
- Joined: Fri Dec 20, 2002 5:37 pm
Question: abort 202 output format...
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 !!!
and there are still some left should be pay attention?
my answer always wrong...
thx for reply !!!
Re: Question: abort 202 output format...
These three examples should do it.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
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]
[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]
Try this input: 1 1htl wrote:My prog alway got WA. Do I misunderstand the meaning of the prob?
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?
It said:
and the digits in [] is more than 50,
where does the "first place" begins?
And should I place the left parenthesis?
But what if there is a expasion like this: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.
Code: Select all
0.[12989874746764.....](.....7696)
where does the "first place" begins?
And should I place the left parenthesis?
-
- Experienced poster
- Posts: 192
- Joined: Sat Nov 30, 2002 5:14 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.
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.
-
- Experienced poster
- Posts: 192
- Joined: Sat Nov 30, 2002 5:14 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.
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.
-
- Experienced poster
- Posts: 192
- Joined: Sat Nov 30, 2002 5:14 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.
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.
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm