2011年6月15日 星期三

ACM 694 The Collatz Sequence

不難

#include <stdio.h>

int main(void)
{
    int c, t;
    long long A, L, n;
   
    t = 1;
    while (scanf("%lld%lld", &A, &L) == 2 && (A > 0 || L > 0))
    {
        c = 1;
        n = A;
        while (n != 1)
        {
            if (n % 2)
                n = 3*n+1;
            else
                n /= 2;
            if (n > L)
                break;
            c++;
        }
        printf("Case %d: A = %lld, limit = %lld, number of terms = %d\n", t++, A, L, c);
    }

    return 0;
}

沒有留言:

張貼留言