Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com
And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Thursday, December 25, 2014

DEADFR-Dead Fraction

Dead Fraction

Below given code is for deadfr spoj or dead fraction spoj.

Source : - http://www.basic-mathematics.com/converting-repeating-decimals-to-fractions.html

How this problem can be solved :- 

Step 1:

Let x equal the repeating decimal you are trying to convert to a fraction

Step 2:

Examine the repeating decimal to find the repeating digit(s)

Step 3:

Place the repeating digit(s) to the left of the decimal point

Step 4:

Place the repeating digit(s) to the right of the decimal point

Step 5:

Subtract the left sides of the two equations.Then, subtract the right sides of the two equations

As you subtract, just make sure that the difference is positive for both sides

Now let's practice converting repeating decimals to fractions with two good examples

Example #1:

What rational number or fraction is equal to 0.55555555555

Step 1:

x = 0.5555555555 

Step 2:

After examination, the repeating digit is 5

Step 3:

To place the repeating digit ( 5 ) to the left of the decimal point, you need to move the decimal point 1 place to the right

repeating-decimals-image


Technically, moving a decimal point one place to the right is done by multiplying the decimal number by 10.

When you multiply one side by a number, you have to multiply the other side by the same number to keep the equation balanced

Thus, 10x = 5.555555555

Step 4:

Place the repeating digit(s) to the right of the decimal point

Look at the equation in step 1 again. In this example, the repeating digit is already to the right, so there is nothing else to do.

x = 0.5555555555

Step 5:

Your two equations are:

10x = 5.555555555

   x = 0.5555555555

10x - x = 5.555555555 − 0.555555555555

9x = 5

Divide both sides by 9

x = 5/9


What you have to consider in problem ?

1. If given fraction is 0.2154... then
        Case 1-  '4' can be repeating 
        Case 2- '54' can be repeating 
        Case 3- '154' can be repeating 
        Case 4- '2154' can also be repeating  
So you have to check for all case and print the one with lowest denominator.

2. If ans is '1' then print "1/1" .





#include <bits/stdc++.h>
using namespace std;
#define ULL unsigned long long
ULL pow_10[19] = {1 , 10 , 100 , 1000 , 10000 , 100000 , 1000000 , 10000000 , 100000000 , 1000000000 , 10000000000LL , 100000000000LL , 1000000000000LL , 10000000000000LL , 100000000000000LL , 1000000000000000LL , 10000000000000000LL , 100000000000000000LL , 1000000000000000000LL};
int main()
{
    while(true){
        char s[30] ;
        scanf("%s",s);
        if(s[0] == '0' && strlen(s) == 1)
            break;
        int len = strlen(s) - 5;
        int conslen = len;
        ULL num = 0 , consnum, denomeator , temp;
        for(int i = 0 , j = 2; i  < len ;j++, i++)
            num = num*10 + s[j] - 48;
        consnum = num;
        ULL nume = 999999999999999999LL , deno = 999999999999999999LL;
        for(int i = 0 ; i<conslen ; i++){
            temp = consnum - num / 10;
            denomeator = pow_10[conslen] - pow_10[len - 1];
            ULL gcd = __gcd(temp , denomeator);
            if((denomeator / gcd) < deno){
                deno = denomeator / gcd , nume = temp / gcd;
            }
            num/=10;
            len--;
        }
        printf("%llu/%llu\n", nume, deno);
    }
    return 0;
}