Please help me out with this code…
or please explain me how the output of 100 is 120?
#include<stdio.h>
unsigned long long int t=1000000000,n,ans=0,s1,s2,s3,previous=0;
int main()
{
while(scanf("%llu",&n)!=EOF)
{
s1=s2=s3=n;
if(n>=0 && n<=t)
{
previous=n;
ans=s1/2+s2/3+s3/4;
s1=s1/2;s2=s2/3;s3=s3/4;
while(ans>previous)
{
previous=ans;
s1=s2=s3=ans;
s1=s1/2;s2=s2/3;s3=s3/4;
ans=s1+s2+s3;
}
printf("%llu\n",previous);
}
}
return 0;
}
you are printing some extra value as well…check ur code again
You are printing some extra values.
please explain me how the output of 100 is 120?
The problem is a good dynamic programming example.You are missing the fact that you can further exchange the obtained coins for more coins.
ok.
I have changed my program according to what you are saying.
Now where is the mistake.?
#include<stdio.h>
unsigned long long int t=1000000000,n,ans=0,s1,s2,s3,previous=0,t1,t2,t3;
int main()
{
while(scanf("%llu",&n)!=EOF)
{
previous=s1=s2=s3=n;
if(n>=0 && n<=t)
{
s1=s1/2;s2=s2/3;s3=s3/4;
ans=s1+s2+s3;
while(ans>previous)
{
previous=ans;
t1=counter(s1);
t2=counter(s2);
t3=counter(s3);
s1=t1;
s2=t2;
s3=t3;
ans=t1+t2+t3;
}
printf("%llu\n",previous);
}
}
return 0;
}
int counter(unsigned long long int a1)
{
return (a1/2 + a1/3 + a1/4);
}
You are returning the int from the counter function which is not correct that’s why it will be providing garbage values into the variables.