2009年2月15日 星期日

Q694: The Collatz Sequence

/*
The same as Q100
Run time: 0.050
*/
#include <stdio.h>
int calculate(long long int n, long long int limit);
int main()
{
long long int num, limit;
int count;
int cnt = 0;
while (scanf("%lld%lld", &num, &limit))
{
if (num < 0 && limit < 0)
break;

count = calculate(num, limit);

printf("Case %d: A = %lld, limit = %lld, number of terms = %d\n", ++cnt, num, limit, count);

}
}

int calculate(long long int n, long long int limit)
{
int len = 1;
while (n != 1)
{
n = n%2 ? 3*n+1 : n/2;

if (n > limit)
return len;
len++;
}
return len;
}

沒有留言: