Problem Type : DP
#include<stdio.h>
long int generate(long int p,long int count)
{
int n=0;
if(p==0)
return count;
else if(p==1)
{
p=p-1;
count++;
return generate(p,count);
}
else if(p==3)
{
p=p-1;
count++;
return generate(p,count);
}
else if(p%2==0)
{
p=p/2;
count++;
return generate(p,count);
}
if(p%2==1)
{
n=p/2;
if(n%2==1)
{
p=p+1;
count++;
return generate(p,count);
}
else if(n%2==0)
{
p=p-1;
count++;
return generate(p,count);
}
}
}
int main()
{
long int n,i,j,p,sum;
while(scanf("%ld",&n)==1)
{
p=n;
j=0;
sum= generate(p,j);
printf("%ld\n",sum);
}
return 0;
}
#include<stdio.h>
long int generate(long int p,long int count)
{
int n=0;
if(p==0)
return count;
else if(p==1)
{
p=p-1;
count++;
return generate(p,count);
}
else if(p==3)
{
p=p-1;
count++;
return generate(p,count);
}
else if(p%2==0)
{
p=p/2;
count++;
return generate(p,count);
}
if(p%2==1)
{
n=p/2;
if(n%2==1)
{
p=p+1;
count++;
return generate(p,count);
}
else if(n%2==0)
{
p=p-1;
count++;
return generate(p,count);
}
}
}
int main()
{
long int n,i,j,p,sum;
while(scanf("%ld",&n)==1)
{
p=n;
j=0;
sum= generate(p,j);
printf("%ld\n",sum);
}
return 0;
}
No comments:
Post a Comment