Page **6** of **6**

### Re: 202-why WA?

Posted: **Thu Sep 22, 2011 8:44 am**

by **plamplam**

Code: Select all

```
15/3 = 5.(0)
1 = number of digits in repeating cycle
1/2971 = 0.(00033658700774150117805452709525412319084483338943...)
2970 = number of digits in repeating cycle
```

### Re: UVA 202

Posted: **Thu Sep 22, 2011 8:45 am**

by **plamplam**

Code: Select all

```
15/3 = 5.(0)
1 = number of digits in repeating cycle
1/2971 = 0.(00033658700774150117805452709525412319084483338943...)
2970 = number of digits in repeating cycle
```

### Re: 202 - Repeating Decimals

Posted: **Thu Mar 26, 2015 12:42 pm**

by **atul_pust**

Why WA?? I have checked all test case from this forum. But I really can't figure out the bug. please help me anyone.

My code is given below :

Code: Select all

```
#include <iostream>
#include <string>
#include <map>
using namespace std;
string ans = "", ans2, ans3;
map<int, int> M, pos;
int vagfol, mod, p, len, l;
bool flag = false;
void find_cycle(int a, int b){
if(a < b){
a *= 10;
}
while(a < b){
ans += '0';
a *= 10;
}
if(M[a] == 1){
p = pos[a];
return;
}
M[a] = 1;
pos[a] = ans.size();
mod = a % b;
int d = a / b;
string number = "";
while(d != 0){
int r = d % 10;
char c = r + 48;
number += c;
d /= 10;
}
for(int i = number.size() - 1; i >= 0; i--){
ans += number[i];
}
if(M[mod] == 1){
p = pos[mod];
return;
}
if(mod == 0){
flag = true;
p = 0;
return;
}
M[mod] = 1;
pos[mod] = ans.size();
find_cycle(mod, b);
}
void add_dot(int a, int b){
if(a < b){
vagfol = 0;
}
else{
vagfol = a / b;
a = a % b;
}
if(a == 0){
flag = true;
return ;
}
if(a < b){
M[a] = 1;
pos[a] = ans.size();
a *= 10;
}
while(a < b){
ans += '0';
a *= 10;
}
find_cycle(a, b);
}
int main()
{
int a, b, s, e;
while(cin >> a >> b){
s = a;
e = b;
vagfol = 0;
M.clear();
pos.clear();
ans = "";
ans2 = "";
ans3 = "";
flag = false;
add_dot(a, b);
if(flag){
l = 1;
ans2 = ans;
ans3 = "0";
}
else{
len = ans.size(), l = ans.size();
for(int i = 0; i < p; i++)
ans2 += ans[i];
if(len > 50){
ans3 = ans.substr(p, 50);
ans3 += "...";
}
else
ans3 = ans.substr(p, ans.size() - p);
}
cout << s << "/" << e << " = " << vagfol << ".";
if(ans2.size() != 0)
cout << ans2 ;
cout << "(" <<ans3 << ")" << endl;
cout << " " << l - p << " = number of digits in repeating cycle\n\n";
}
return 0;
}
```

### Re: 202 - Repeating Decimals

Posted: **Fri Mar 27, 2015 3:59 am**

by **Tiramisu**

to atul_pust

2nd line of output should have three preceding spaces wheres your code has only one.

76/25 = 3.04(0)

^^^1 = number of digits in repeating cycle

### Re: 202 - Repeating Decimals

Posted: **Fri Mar 27, 2015 7:18 am**

by **atul_pust**

to Tiramisu

I have used three spaces in second line. But WA remains. I need more test cases where my code fails to correct output.

Thanks in advance.

### Re: 202 - Repeating Decimals

Posted: **Fri Mar 27, 2015 8:05 am**

by **Tiramisu**

try input

1 2222

correct output:

1/2222 = 0.0(0045)

4 = number of digits in repeating cycle

but your output:

1/2222 = 0.000(4500)

4 = number of digits in repeating cycle

seems like you are checking for a cyclic pattern from right to left, but it should be from left to right.

### Give WA rather than PE for C++11, too

Posted: **Sun Feb 28, 2016 8:31 am**

by **liangshiyu**

little joey wrote: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've got WA for 5 times, then after I added a newline at the end, I got AC.

The OJ always gives WA rather than PE for C++11 as it does for pascal.

Another tip is try 1 251 as input, you shouln't ouput "...".(in fact, if n is a prime, the length of repeating cycle of 1/n is a divisor of (n-1) according to others, but I haven't proved it)

### Re: 202 - Repeating Decimals

Posted: **Wed Mar 23, 2016 5:34 pm**

by **metaphysis**

Same to me.