The Collatz Sequence

#include <stdio.h>
int main()
{
    long a, b, i, j = 0, m;
    while(scanf("%ld%ld", &a, &b)!=EOF)
    {
        m = a;
        i = 1;
        if(a < 0 && b < 0) break;
        else
        {
            while(a != 1)
            {
                if(a > b)
                {
                    i--;
                    break;
                }
                else
                {
                if(a % 2 == 0)
                {
                    a = a / 2;
                    i++;
                }
                else
                {
                    a = 3 * a + 1;
                    i++;
                }
                }
            }
        }
        j++;
        printf("Case %ld: A = %ld, limit = %ld, number of terms = %ld\n", j, m, b, i);
    }
    return 0;
}

你可能感兴趣的:(编程,C语言)