Guess the Number
Link to the question : GUESSTHE
HINT :
We need to find the LCM of the numbers which are given a Y, and then check for the numbers which are given N, whether they divide the LCM.
RECOMMENNDED QUESTION:
You would surely love solving this question after trying your hands in this one.SOURCE CODE :
#include<stdio.h>
long long int gcd(long long int a,long long int b)
{
if(b>a)
return gcd(b,a);
else if(b==0)
return a;
else
return gcd(b,a%b);
}
long long int lcm(long long int a,long long int b)
{
return a*b/gcd(a,b);
}
int main()
{
char c,a[22];
while(1) {
int i=1,j=0;
long long int k=1;
scanf("%c",&c);
while(c!='\n'&&c!='*')
{
if(c=='Y')
k= lcm(k,i);
else if(c=='N')
a[j++]= i;
i++;
scanf("%c",&c);
}
if(c=='*')
return 0;
else {
for(i=0;i<j;i++)
{
if(k%a[i]==0)
{
k=-1;
break;
}
}
printf("%lld\n",k);
}
}
}
No comments:
Post a Comment