"50th decimal places" should be counted from the decimal point, or from the first digit of the repeating cycle ?
Could anyone give me some more test cases other than those have been posted in the past posts ?
202 - Repeating Decimals
Moderator: Board moderators
-
- Experienced poster
- Posts: 192
- Joined: Sat Nov 30, 2002 5:14 am
Code: Select all
Test Cases:
Input:
2099 1
20 20
10 9
1001 998
890 901
2999 928
927 123
123 123
1 1
0 20
0 1000
1 1099
1 2999
2999 789
865 2993
399 1001
998 97
73 41
41 569
567 789
12 1992
1999 71
711 153
Output:
2099/1 = 2099.(0)
1 = number of digits in repeating cycle
20/20 = 1.(0)
1 = number of digits in repeating cycle
10/9 = 1.(1)
1 = number of digits in repeating cycle
1001/998 = 1.0(0300601202404809619238476953907815631262525050100...)
498 = number of digits in repeating cycle
890/901 = 0.(98779134295227524972253052164261931187569367369589...)
208 = number of digits in repeating cycle
2999/928 = 3.23168(1034482758620689655172413793)
28 = number of digits in repeating cycle
927/123 = 7.(53658)
5 = number of digits in repeating cycle
123/123 = 1.(0)
1 = number of digits in repeating cycle
1/1 = 1.(0)
1 = number of digits in repeating cycle
0/20 = 0.(0)
1 = number of digits in repeating cycle
0/1000 = 0.(0)
1 = number of digits in repeating cycle
1/1099 = 0.(00090991810737033666969972702456778889899909008189...)
78 = number of digits in repeating cycle
1/2999 = 0.(00033344448149383127709236412137379126375458486162...)
1499 = number of digits in repeating cycle
2999/789 = 3.(80101394169835234474017743979721166032953105196451...)
262 = number of digits in repeating cycle
865/2993 = 0.(2890076845973939191446708987637821583695)
40 = number of digits in repeating cycle
399/1001 = 0.(398601)
6 = number of digits in repeating cycle
998/97 = 10.(28865979381443298969072164948453608247422680412371...)
96 = number of digits in repeating cycle
73/41 = 1.(78048)
5 = number of digits in repeating cycle
41/569 = 0.(07205623901581722319859402460456942003514938488576...)
284 = number of digits in repeating cycle
567/789 = 0.(71863117870722433460076045627376425855513307984790...)
262 = number of digits in repeating cycle
12/1992 = 0.0(06024096385542168674698795180722891566265)
41 = number of digits in repeating cycle
1999/71 = 28.(15492957746478873239436619718309859)
35 = number of digits in repeating cycle
711/153 = 4.(6470588235294117)
16 = number of digits in repeating cycle


-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
Eric: Your WA is in fact a runtime error caused by extra empty lines at the end of the input. The statement Readln (Nu, De); tries to read the values, but there are no more left to read.
I added a line to your code and got accepted:
[pascal]Begin
While not eof Do
Begin
if eoln then begin readln; continue; end;
Readln (Nu, De);
...[/pascal]
This line just eats the empty line and loops back to the while-statement that checks for an end-of-file.
It is very irritating that the online judge doesn't report runtime errors for Pascal programs, but gives Wrong Answer instead. I have reported this long time ago, but nobody seems to care. So always be aware that your WA is in fact a runtime error.
I added a line to your code and got accepted:
[pascal]Begin
While not eof Do
Begin
if eoln then begin readln; continue; end;
Readln (Nu, De);
...[/pascal]
This line just eats the empty line and loops back to the while-statement that checks for an end-of-file.
It is very irritating that the online judge doesn't report runtime errors for Pascal programs, but gives Wrong Answer instead. I have reported this long time ago, but nobody seems to care. So always be aware that your WA is in fact a runtime error.
-
- New poster
- Posts: 39
- Joined: Wed Jan 22, 2003 11:02 am
202 WA.
[cpp]
#include <iostream.h>
int up,down;
int i,j,k;
int q[10000],r[10000];
int ko;
void main(void)
{
while(cin>>up>>down)
{
cout<<up<<"/"<<down<<" = ";
if (up % down==0)
{
cout<<up/down<<".(0)"<<endl;
cout<<" 1 = number of digits in repeating cycle"<<endl;
continue;
}
if (up>down)
{
cout<<up/down<<".";
}
else
{
}
cout<<"0.";
ko=0;
r[0]=up % down;
for (i=1;i<10000;i++)
{
if (r[i-1]*10<down)
{
q[i-1]=0;
r=r[i-1]*10;
}
else
{
q[i-1]=r[i-1]*10 /down;
r= r[i-1]*10 % down;
}
if (r==0 && i<=50)
{
ko=1;
break;
}
if (r==0 && i>50)
{
ko=2;
break;
}
for (j=0;j<i;j++)
{
if (r==r[j] )
{
if (i<=50)
{
ko=3;
break;
}
else
{
ko=4;
break;
}
}
}
if (ko==3||ko==4)
break;
}
if (ko==1)
{
for (k=0;k<i;k++)
cout<<q[k];
cout<<"(0)"<<endl;
cout<<" "<<i-j<<" = number of digits in repeating cycle"<<endl;
}
if (ko==2)
{
for (k=0;k<50;k++)
cout<<q[k];
cout<<"..."<<endl;
cout<<" "<<i-j<<" = number of digits in repeating cycle"<<endl;
}
if ( ko==3)
{
for (k=0;k<j;k++)
cout<<q[k];
cout<<"(";
for (k=j;k<i;k++)
cout<<q[k];
cout<<")"<<endl;
cout<<" "<<i-j<<" = number of digits in repeating cycle"<<endl;
}
if (ko==4)
{
for (k=0;k<j;k++)
cout<<q[k];
cout<<"(";
for (k=j;k<50;k++)
cout<<q[k];
cout<<"...)"<<endl;
cout<<" "<<i-j<<" = number of digits in repeating cycle"<<endl;
}
cout<<"\n";
}
}
[/cpp]
What's matter?
#include <iostream.h>
int up,down;
int i,j,k;
int q[10000],r[10000];
int ko;
void main(void)
{
while(cin>>up>>down)
{
cout<<up<<"/"<<down<<" = ";
if (up % down==0)
{
cout<<up/down<<".(0)"<<endl;
cout<<" 1 = number of digits in repeating cycle"<<endl;
continue;
}
if (up>down)
{
cout<<up/down<<".";
}
else
{
}
cout<<"0.";
ko=0;
r[0]=up % down;
for (i=1;i<10000;i++)
{
if (r[i-1]*10<down)
{
q[i-1]=0;
r=r[i-1]*10;
}
else
{
q[i-1]=r[i-1]*10 /down;
r= r[i-1]*10 % down;
}
if (r==0 && i<=50)
{
ko=1;
break;
}
if (r==0 && i>50)
{
ko=2;
break;
}
for (j=0;j<i;j++)
{
if (r==r[j] )
{
if (i<=50)
{
ko=3;
break;
}
else
{
ko=4;
break;
}
}
}
if (ko==3||ko==4)
break;
}
if (ko==1)
{
for (k=0;k<i;k++)
cout<<q[k];
cout<<"(0)"<<endl;
cout<<" "<<i-j<<" = number of digits in repeating cycle"<<endl;
}
if (ko==2)
{
for (k=0;k<50;k++)
cout<<q[k];
cout<<"..."<<endl;
cout<<" "<<i-j<<" = number of digits in repeating cycle"<<endl;
}
if ( ko==3)
{
for (k=0;k<j;k++)
cout<<q[k];
cout<<"(";
for (k=j;k<i;k++)
cout<<q[k];
cout<<")"<<endl;
cout<<" "<<i-j<<" = number of digits in repeating cycle"<<endl;
}
if (ko==4)
{
for (k=0;k<j;k++)
cout<<q[k];
cout<<"(";
for (k=j;k<50;k++)
cout<<q[k];
cout<<"...)"<<endl;
cout<<" "<<i-j<<" = number of digits in repeating cycle"<<endl;
}
cout<<"\n";
}
}
[/cpp]
What's matter?
-
- Learning poster
- Posts: 90
- Joined: Sat Feb 15, 2003 1:39 am
- Location: Paris, France
- Contact:
-
- New poster
- Posts: 39
- Joined: Wed Jan 22, 2003 11:02 am
-
- Learning poster
- Posts: 82
- Joined: Thu Oct 10, 2002 1:15 pm
- Location: St. Johns, Canada
- Contact:
202 - Why WA
I think my program gives correct output. But it gets WA. I don't know why. There may be some problem in output format or anything else. Can anyone tell me why?
Here is my code.
[cpp]
#include<stdio.h>
#include<math.h>
#define MAX 5000
int ls(int *a,int key, int n)
{
register int i;
for(i = 0; i< n; i++)
if(a == key)
return i;
return -1;
}
main(void)
{
int nom,denom,orinom,oridenom;
int bp,t,i,j;
char num[MAX];
while(scanf("%d%d",&nom,&denom)!=EOF)
{
orinom = nom;
oridenom = denom;
i = 0;
int a[MAX] = {0};
if(nom>denom)
{
bp = floor((double)nom/denom);
nom = nom % denom;
}
else
bp = 0;
nom *= 10;
while(1)
{
if(!nom)
break;
while(nom < denom)
{
t = ls(a,nom,i+1);
if(t!=-1)
break;
a = nom;
num[i++] = 0;
nom *= 10;
}
t = ls(a,nom,i+1);
if(t == -1)
{
a = nom;
num = floor((double)nom/denom);
nom = nom%denom;
nom*=10;
i++;
}
else break;
}
printf("%d/%d = %d.",orinom,oridenom,bp);
if( t == -1)
{
for(j = 0; j< i; j++)
putchar(num[j]+'0');
printf("(0)\n 1 = number of digits in repeating cycle");
}
else
{
for(j = 0; j< t; j++)
putchar(num[j]+'0');
putchar('(');
for( j = t; j < i ; j++)
{
if(j==50)
{
printf("...");
break;
}
putchar(num[j]+'0');
}
printf(")\n %d = number of digits in repeating cycle",i-t);
}
putchar('\n');
putchar('\n');
}
return 0;
}
[/cpp]
M H Rasel
CUET Old Sailor
Here is my code.
[cpp]
#include<stdio.h>
#include<math.h>
#define MAX 5000
int ls(int *a,int key, int n)
{
register int i;
for(i = 0; i< n; i++)
if(a == key)
return i;
return -1;
}
main(void)
{
int nom,denom,orinom,oridenom;
int bp,t,i,j;
char num[MAX];
while(scanf("%d%d",&nom,&denom)!=EOF)
{
orinom = nom;
oridenom = denom;
i = 0;
int a[MAX] = {0};
if(nom>denom)
{
bp = floor((double)nom/denom);
nom = nom % denom;
}
else
bp = 0;
nom *= 10;
while(1)
{
if(!nom)
break;
while(nom < denom)
{
t = ls(a,nom,i+1);
if(t!=-1)
break;
a = nom;
num[i++] = 0;
nom *= 10;
}
t = ls(a,nom,i+1);
if(t == -1)
{
a = nom;
num = floor((double)nom/denom);
nom = nom%denom;
nom*=10;
i++;
}
else break;
}
printf("%d/%d = %d.",orinom,oridenom,bp);
if( t == -1)
{
for(j = 0; j< i; j++)
putchar(num[j]+'0');
printf("(0)\n 1 = number of digits in repeating cycle");
}
else
{
for(j = 0; j< t; j++)
putchar(num[j]+'0');
putchar('(');
for( j = t; j < i ; j++)
{
if(j==50)
{
printf("...");
break;
}
putchar(num[j]+'0');
}
printf(")\n %d = number of digits in repeating cycle",i-t);
}
putchar('\n');
putchar('\n');
}
return 0;
}
[/cpp]
M H Rasel
CUET Old Sailor
-
- New poster
- Posts: 3
- Joined: Sun Oct 05, 2003 1:32 pm
- Location: Campinas-SP
- Contact:
202 - Output Limit Exceeded
By an overview on the main code, does anyone see anuthing wrong???
Dear Thiago Serra:
Your program output is greater (4206592 bytes) than the maximum
allowed for any problem by this judge (4194304 bytes).
You must interprete this as a Wrong Answer (in fact, is wrong!).
By some reason your program is writing an unlimited output.
--
[c]
main() {
int div,num;
int in,res;
int vetor[3000];
int i;
scanf("%d",&num);
while (num!=EOF) {
scanf("%d",&div);
for (i=0;i<div;i++)
vetor=NECA;
in=num/div;
res=num%div;
comeco=res;
printf("%d/%d = %d.",num,div,in);
resolver(res,div,vetor);
scanf("%d",&num);
}
}
[/c][quote][/quote]
Dear Thiago Serra:
Your program output is greater (4206592 bytes) than the maximum
allowed for any problem by this judge (4194304 bytes).
You must interprete this as a Wrong Answer (in fact, is wrong!).
By some reason your program is writing an unlimited output.
--
[c]
main() {
int div,num;
int in,res;
int vetor[3000];
int i;
scanf("%d",&num);
while (num!=EOF) {
scanf("%d",&div);
for (i=0;i<div;i++)
vetor=NECA;
in=num/div;
res=num%div;
comeco=res;
printf("%d/%d = %d.",num,div,in);
resolver(res,div,vetor);
scanf("%d",&num);
}
}
[/c][quote][/quote]
"Dust in the wind... Everything is dust in the wind"(Kansas)
"Hay que endurecer, pero sin perder la ternura jamas!"(Che Guevara)
"Hay que endurecer, pero sin perder la ternura jamas!"(Che Guevara)
You should use:
[c]while (scanf ("%d", &num) == 1)
{
...
}[/c]
The file is ended by EOF. scanf will not interpret this as a number, and thus does not change num when it reaches EOF, which causes the infinite loop. The above makes sure scanf is reading 1 integer, not more, not less, so when it reaches EOF, it will exit the loop.
[c]while (scanf ("%d", &num) == 1)
{
...
}[/c]
The file is ended by EOF. scanf will not interpret this as a number, and thus does not change num when it reaches EOF, which causes the infinite loop. The above makes sure scanf is reading 1 integer, not more, not less, so when it reaches EOF, it will exit the loop.
me also WA in 202
I too keep getting WA when my code seemed to pass all the test cases suggested by a former post related to this problem. Any tricky test case to suggest? Thanks for any help!!
[cpp]/* 202 */
#include <iostream.h>
#include <stdlib.h>
#include <string>
int main()
{
int numer, denom;
while(true)
{
cin >> numer >> denom;
if(cin.eof())
break;
int index=0;
int appeared[3001];
int digit[3001];
int start = -1, end = -1;
int cyclelen = -1;
cout << numer << "/" << denom << " = ";
cout << numer/denom << ".";
numer = numer%denom;
numer*=10;
while(true)
{
for(int i=0; i<index; i++)
{
if(appeared == numer)
{
start = i;
end = index;
cyclelen = index - i;
break;
}
}
if(start >= 0) // found
break;
if(numer%denom == 0)
{
digit[index] = numer/denom;
start = index;
index++;
digit[index] = 0;
end = index;
cyclelen = 1;
break;
}
else if((numer/denom) > 0)
{
appeared[index] = numer;
digit[index] = numer / denom;
index++;
numer = numer%denom;
}
else
{
appeared[index] = numer;
digit[index] = 0;
index++;
}
numer = 10*numer;
}
for(int k=0; k<10000; k++)
{
if(k == start)
cout << "(";
if(k == start+50 && k!=end)
cout << "...";
if(k == start+50 || k == end)
{
cout << ")";
break;
}
cout << digit[k];
}
cout << "\t\n" << cyclelen << " = number of digits in repeating cycle\n\n";
//cout << "start: " << start << "\tend: " << end << "\n";
}
return 0;
}[/cpp]
[cpp]/* 202 */
#include <iostream.h>
#include <stdlib.h>
#include <string>
int main()
{
int numer, denom;
while(true)
{
cin >> numer >> denom;
if(cin.eof())
break;
int index=0;
int appeared[3001];
int digit[3001];
int start = -1, end = -1;
int cyclelen = -1;
cout << numer << "/" << denom << " = ";
cout << numer/denom << ".";
numer = numer%denom;
numer*=10;
while(true)
{
for(int i=0; i<index; i++)
{
if(appeared == numer)
{
start = i;
end = index;
cyclelen = index - i;
break;
}
}
if(start >= 0) // found
break;
if(numer%denom == 0)
{
digit[index] = numer/denom;
start = index;
index++;
digit[index] = 0;
end = index;
cyclelen = 1;
break;
}
else if((numer/denom) > 0)
{
appeared[index] = numer;
digit[index] = numer / denom;
index++;
numer = numer%denom;
}
else
{
appeared[index] = numer;
digit[index] = 0;
index++;
}
numer = 10*numer;
}
for(int k=0; k<10000; k++)
{
if(k == start)
cout << "(";
if(k == start+50 && k!=end)
cout << "...";
if(k == start+50 || k == end)
{
cout << ")";
break;
}
cout << digit[k];
}
cout << "\t\n" << cyclelen << " = number of digits in repeating cycle\n\n";
//cout << "start: " << start << "\tend: " << end << "\n";
}
return 0;
}[/cpp]
202 - Why WA, Please help
I've read all the topics about problem 202.
But nothing helped me to find the bug in my program.
Just take a look, please.
I think my bug is in output formatting.
I'll be greatful if you tell me a test data that my program dosn't solve.
[pascal]
const
lngmax = 6200;
var
digits : array [1..lngmax] of byte;
hm0,ht0,x, smbs, ps,pn, nd, i, j, hm, ht : longint;
b : boolean;
s : longint;
function min(a,b:integer):integer;
begin
if a > b then
min := b
else
min := a;
end;
procedure push(a : longint);
begin
nd := nd + 1;
digits[nd] := a;
if (a <> 10) then
s := s + a;
end;
procedure add(n : longint);
var
digit : array [1..15] of byte;
i, m : longint;
begin
if (n = 0) then
push(0)
else
begin
m := 0;
while n <> 0 do
begin
m := m + 1;
digit[m] := n mod 10;
n := n div 10;
end;
for i := m downto 1 do
push(digit)
end;
end;
begin
while not eof do
begin
if eoln then
begin
readln;
continue;
end;
ps := -1;
nd := 0;
b := false;
readln(hm,ht);
hm0 := hm;
ht0 := ht;
s := hm;
if (ht < hm) then
s := ht;
s := 0;
if (hm mod ht = 0) then
begin
writeln(hm div ht,'.(0)');
writeln(' 1 = number of digits in repeating cycle');
end
else
begin
add(hm div ht);
hm := hm mod ht;
push(10);
while nd < lngmax do
if (hm div ht = 0) then
begin
if b then
add(0)
else
b := true;
hm := hm * 10;
end
else
begin
add(hm div ht);
hm := hm mod ht;
b := false;
end;
smbs := 0;
i := 1;
while digits <> 10 do
i := i + 1;
i := i + 1;
b := true;
while (i <= 80) and b do
begin
for j := i + 1 to lngmax-1 do
if (digits = digits[j]) and
(digits[i+1] = digits[j+1]) and
(digits[i+2] = digits[j+2]) and
(digits[i+3] = digits[j+3]) and
(digits[i+4] = digits[j+4]) and
(digits[i+5] = digits[j+5]) then
begin
ps := i-1;
pn := j-1;
b := false;
break;
end;
i := i + 1;
end;
write(hm0,'/',ht0,' = ');
for i := 1 to ps do
begin
if (digits = 10) then
begin
x := i;
write('.')
end
else
write(digits);
inc(smbs);
end;
write('(');
smbs := smbs + 1;
for i := ps +1 to min(pn,x+50) do
begin
write(digits);
end;
if (x + 50 < pn) then
write('...');
write(')');
writeln;
writeln(' ',pn-ps,' = number of digits in repeating cycle');
writeln;
end;
end;
end.
[/pascal]
But nothing helped me to find the bug in my program.
Just take a look, please.
I think my bug is in output formatting.
I'll be greatful if you tell me a test data that my program dosn't solve.
[pascal]
const
lngmax = 6200;
var
digits : array [1..lngmax] of byte;
hm0,ht0,x, smbs, ps,pn, nd, i, j, hm, ht : longint;
b : boolean;
s : longint;
function min(a,b:integer):integer;
begin
if a > b then
min := b
else
min := a;
end;
procedure push(a : longint);
begin
nd := nd + 1;
digits[nd] := a;
if (a <> 10) then
s := s + a;
end;
procedure add(n : longint);
var
digit : array [1..15] of byte;
i, m : longint;
begin
if (n = 0) then
push(0)
else
begin
m := 0;
while n <> 0 do
begin
m := m + 1;
digit[m] := n mod 10;
n := n div 10;
end;
for i := m downto 1 do
push(digit)
end;
end;
begin
while not eof do
begin
if eoln then
begin
readln;
continue;
end;
ps := -1;
nd := 0;
b := false;
readln(hm,ht);
hm0 := hm;
ht0 := ht;
s := hm;
if (ht < hm) then
s := ht;
s := 0;
if (hm mod ht = 0) then
begin
writeln(hm div ht,'.(0)');
writeln(' 1 = number of digits in repeating cycle');
end
else
begin
add(hm div ht);
hm := hm mod ht;
push(10);
while nd < lngmax do
if (hm div ht = 0) then
begin
if b then
add(0)
else
b := true;
hm := hm * 10;
end
else
begin
add(hm div ht);
hm := hm mod ht;
b := false;
end;
smbs := 0;
i := 1;
while digits <> 10 do
i := i + 1;
i := i + 1;
b := true;
while (i <= 80) and b do
begin
for j := i + 1 to lngmax-1 do
if (digits = digits[j]) and
(digits[i+1] = digits[j+1]) and
(digits[i+2] = digits[j+2]) and
(digits[i+3] = digits[j+3]) and
(digits[i+4] = digits[j+4]) and
(digits[i+5] = digits[j+5]) then
begin
ps := i-1;
pn := j-1;
b := false;
break;
end;
i := i + 1;
end;
write(hm0,'/',ht0,' = ');
for i := 1 to ps do
begin
if (digits = 10) then
begin
x := i;
write('.')
end
else
write(digits);
inc(smbs);
end;
write('(');
smbs := smbs + 1;
for i := ps +1 to min(pn,x+50) do
begin
write(digits);
end;
if (x + 50 < pn) then
write('...');
write(')');
writeln;
writeln(' ',pn-ps,' = number of digits in repeating cycle');
writeln;
end;
end;
end.
[/pascal]