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
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; }
number must be initialise to 0.
ReplyDeleteSo you are leaving any thought of recurring decimal.
ReplyDeleteAccording to this code, the answer for 0.3333 will be 10000 but it can be 4/3.