Tuesday, 26 March 2013

HOW MANY GAMES (SPOJ)


PROBLEM CODE :GAMES
12448. HOW MANY GAMES
Write the average score x as a reduced fraction x=pq. This means that p and q are integers, that q is positive and that q is minimal (or, equivalently, that p and q have no nontrivial common factor). Then the player can have played any multiple of q games hence the minimum number of games the player should have played is q.

When x=−30.25, note that −30.25=−1214 and −121 and 4 have no common factors except +1 and −1, hence the minimum number of games is indeed 4.

SOLUTION//


#include"stdio.h"
int gcd(int a,int b)
{
    if(b==0)
        return a;
    else
        gcd(b,a%b);
}
int main()
{
int t,num,count,i,j,len,pow,flag;
char s[50];
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
len=strlen(s);
count=0,flag=1;
for(j=len-1;j>=0;j--)
{
if(s[j]=='.'){
flag=0;
break;
}
else
count++;
}
num=0;
for(j=0;j
{
if(s[j]!='.')
num=10*num+(s[j]-'0');
}
pow=1;
if(flag==0){
for(i=0;i
{
pow=pow*10;
}
}
//printf("%d %d",num,pow);
printf("%d\n",pow/gcd(num,pow));
}
return 0;
}

2 comments:

faceiit said...

can you please tell me whats wrong with this code its runs fine for the test cases i ran but when i submit it tells wrong answer



#include
#include
float a[1000];
int main(){
int n,i,j;

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f",&a[i]);

for(i=0;i<n;i++)
{
if(a[i]==ceil(a[i]))
printf("%d\n",1);
else
for(j=2;j<10000;j++)
{
float x=a[i]*j;
if(x==ceil(x)){
printf("%d\n",j);
break; }
}
}
return 0;

}

CHANDAN SINGH said...

for the test cases
10
10.5
631.0001
167.003
125.100
100.100
10.10
20.10
20.100
123.113
115.112
ur program gave output:
2
6648
333
10
10
10
10
10
177
125
while correct one is:
2
10000
1000
10
10
10
10
10
1000
125