uva 100 The 3n + 1 problem

简单题目,不多说了,纯当练习,注意输入的两个数的大小关系式不确定的。

#include 

int get_loop_len(int n)
{
	int count;

	count = 1;
	while(1)
	{
		if(1 == n)
			break;

		if(n % 2)
			n = n*3+1;
		else
			n = n >> 1;

		count ++;
	}

	return count;
}

int main(void)
{
	int m, n, t, max, loop_len;
	int old_m, old_n;
	int i;

	while(scanf("%d %d",&m,&n) != EOF)
	{
		old_m = m;
		old_n = n;

		if(m > n)
		{
			t = m;
			m = n;
			n = t;
		}

		max = 0;
		for(i=m; i<=n; i++)
		{
			loop_len = get_loop_len(i);
			if(max < loop_len)
				max = loop_len;
		}

		printf("%d %d %d\n", old_m, old_n, max);
	}

	return 0;
}


你可能感兴趣的:(acm)