2011年5月1日 星期日

ACM 100 The 3n + 1 problem

照著做就對了


#include <stdio.h>

int main(void)
{
    int i, j, n, s, c, max, flag;
   
    while (scanf("%d%d", &i, &j) == 2)
    {
        printf("%d %d ", i, j);
        if (i > j)
            n = j, j = i, i = n;
        for (s = i, max = 1; s <= j; s++)
        {
            n = s;
            c = 1;
            while (n != 1)
            {
                if (n%2)
                    n = 3*n+1;
                else
                    n /= 2;
                c++;
            }
            if (c > max)
                max = c;
        }
        printf("%d\n", max);
    }
   
    return 0;
}

沒有留言:

張貼留言