9 / 9
Oct 2019

My code is as follows :

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        char Num[16];
        for (int Idx = 0; Idx < 16; Idx++)
            Num[Idx] = 0;

        cin >> Num;

        int L;
        for (L = 0; Num[L] != '.' && L < 16; L++);

        int Dec = 0;
        if (L == 16)
        {
            cout << 1 << endl;
            continue;
        }
        else
        {
            for (int Nxt = 1; Nxt <= 4; Nxt++)
            {
                int DigitVal;
                char Char = Num[L + Nxt];
                if (Char == '\0')
                    DigitVal = 0;
                else
                    DigitVal = Char - '0';
                
                Dec = Dec * 10 + DigitVal;
            }
        }        
        if (Dec == 0)
        {
            cout << 1 << endl;
            continue;
        }
        int GCD = __gcd(10000, Dec);

        cout << 10000 / GCD << endl;
    }

    return 0;
}

It uses string. We get the 4 digits after the decimal point.
I have tested it on the test cases in the other forums, they all work.
Don’t know why its still giving WA.

  • created

    Oct '19
  • last reply

    Oct '19
  • 8

    replies

  • 986

    views

  • 2

    users

  • 1

    link

Did you test it with the example given in the problem? You should try it.

Perhaps you need to take another look then…

I give it:

3
5
5.5
30.25

And it gives me:

10000
2
4

That isn’t the expected output.

For some reason this happens on ideone, but not when I run it locally on my laptop. Will check what’s going on.

It got accepted when I changed the order of evaluation of that condition from :
(Num[L] != '.') && (L < 16) to (L < 16) && (Num[L] != '.')

What in the world is going on?

Well, when L = 16, Num[L] is outside the bounds of the array. Who knows what is going on.

C and C++ are really good like that. they give you all the tools and absolute freedom to shoot yourself in the foot.

Isn’t this a bug. The for loop should end with L=16.