HDU - 1032 The 3n + 1 problem

#include <cstdio>
#include <cstdlib>
#include <climits>
#include <algorithm>
using namespace std;

long long int table[1024000];

void init(void)
{
    for(long long int i=1; i<1000005; i++)
    {
        long long int x = i;
        long long int cnt = 1;
        while(x!=1){
            x = x % 2 == 0 ? (x / 2) : (3 * x + 1);
            if(x<1000005 && table[x]!=0){
                cnt += table[x];
                break;
            }
            cnt++;
        }
        table[i] = cnt;
    }
}

int main()
{
    init();
    int a,b;
    while(~scanf("%d %d",&a,&b))
    {
        int m = INT_MIN;
        int i,temp;
        printf("%d %d ", a, b);
        if(a > b) swap(a, b);
        for(i=a; i<=b; i++)
            m = max((long long int)m, table[i]);
        printf("%d\n",m);
    }
    return 0;
}

posted @ 2015-01-05 23:33  Popco  阅读(134)  评论(0编辑  收藏  举报