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

plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

Re: 202-why WA?

Post by plamplam »

Code: Select all

15 3
1 2971

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

You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson
plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

Re: UVA 202

Post by plamplam »

Code: Select all

15 3
1 2971

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

You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson
atul_pust
New poster
Posts: 3
Joined: Thu Mar 26, 2015 12:14 pm

Re: 202 - Repeating Decimals

Post 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;
}
Tiramisu
New poster
Posts: 8
Joined: Fri Feb 20, 2015 10:28 am

Re: 202 - Repeating Decimals

Post 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
atul_pust
New poster
Posts: 3
Joined: Thu Mar 26, 2015 12:14 pm

Re: 202 - Repeating Decimals

Post 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.
Tiramisu
New poster
Posts: 8
Joined: Fri Feb 20, 2015 10:28 am

Re: 202 - Repeating Decimals

Post 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.
liangshiyu
New poster
Posts: 1
Joined: Sun Feb 28, 2016 7:38 am

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

Post 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)
metaphysis
Experienced poster
Posts: 139
Joined: Wed May 18, 2011 3:04 pm

Re: 202 - Repeating Decimals

Post by metaphysis »

Same to me.
Post Reply

Return to “Volume 2 (200-299)”