Friday, August 12, 2016

Uva 332: Rational Numbers from Repeating Fractions

Problem Link:
                      [https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=268&mosmsg=Submission+received+with+ID+17826944]

Catagory:Mathematics,GCD

Reading material; [http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=math_for_topcoders]


Strategy : First i found out the Denominator and Numerator according to the problem. Then i Calculate the GCD of the values. Then divided Denominator and Numerator by their GCD value. Plz make sure about the Presentation.


Code:

#include<bits/stdc++.h>
#define ll  unsigned long long int
using namespace std;



int main()
{   ll p10[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
     ll j,k,d,n,g,len,t=1;
     double pw;
      string p;
      while(cin>>j)
      {
            if(j==-1)
                  break;
            cin>>p;
           len=p.length()-p.find(".")-1;
             k=len-j;
           if(j)   //if ani repeating digit exist
            d=p10[k+j]-p10[k];
            else    //if not
                  d=p10[k];


            n=0;

           for(ll i=2; i<p.length(); i++)
           {
                 n=n*10 + (p[i]-'0');

           }

           if(j)
           n=n-n/p10[j];

            g=__gcd(n,d);
            cout<<"Case "<<t<<": "<<(ll)n/g<<"/"<<(ll)d/g<<endl;
            t++;
      }


 }

No comments:

Post a Comment