UVa 694 - The Collatz Sequence




/**
 * Author: Gneveek
 * Create Time: 2011-10-1
 * Descripition: UVa 694 - The Collatz Sequence
 */ 
#include <stdio.h>
int main()
{
	long A,limit;
	int terms,cas = 1;
	while(scanf("%ld %ld",&A,&limit))
	{
		if(A < 0 && limit < 0)
			break;
		terms = 1;
		printf("Case %d: A = %ld, ",cas++,A);
		while(A != 1)
		{				
			if(A % 2 == 0)
				A = A / 2;			
			else
				A = 3*A + 1;			
			if(A > limit) break;
			terms++;
		}
		printf("limit = %ld, number of terms = %d\n",limit,terms);	
	}
	return 0;
}


你可能感兴趣的:(UVa 694 - The Collatz Sequence)