UVA – 11567 – Moliu Number Generator   Leave a comment


#include <stdio.h>
int min(int a,int b)
{
	return (a<b)?a:b;
}
int calc(long long int n)
{
	if(n==0)
		return 0;
	else if(n==1)
		return 1;
	else if(n&1)
		return 1 + min(calc(n+1),calc(n-1));
	else
		return 1+calc(n/2);
}
int main()
{
	long long int n;
	while(scanf("%lld",&n)==1)
		printf("%d\n",calc(n));
	return 0;
}

Advertisements

Posted July 4, 2012 by epicrado in Greedy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: