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

Friday, February 7, 2014

GAMES-HOW MANY GAMES

given below code is for games spoj or how many games spoj.
simpe logic behind this is we can write a fraction number into its lowest fraction and we have to just print denominator .
eg:
 lets take 5.5  now we can write this as  55/10 now we calculate the gcd of 55 and 10 which is 5 and then divide the denominator by 5. and  thats the answer

#include <stdio.h>
#include <cmath>
#include <string>
#include <iostream>
using namespace std;
long gcd(long a,long b)
{
 return b==0?a:gcd(b,a%b);
}
int main()
{
 int t,count,i,flage;
 scanf("%d",&t);
 while(t--)
 {
  string s;
  cin>>s;
  long number=1,frac;
  count=0;
  flage=0;
  for(i=s.size()-1;i>=0;i--)
  {
   if(s[i]=='.')
   {
    flage=1;
    break;
   }
   else
    count++;
  }
  for(i=0;i<s.size();i++)
  {
   if(s[i]!='.')
   {
    number=number*10 + (s[i]-48);
   }
  }
  frac=1;
  if(flage)
   frac=pow(10,count);
  printf("%ld\n",frac/gcd(number,frac));
 }
 return 0;
}

2 comments:

  1. number must be initialise to 0.

    ReplyDelete
  2. So you are leaving any thought of recurring decimal.
    According to this code, the answer for 0.3333 will be 10000 but it can be 4/3.

    ReplyDelete

Your comment is valuable to us