【HDOJ】1032 The 3n + 1 problem

【题目】 http://acm.hdu.edu.cn/showproblem.php?pid=1032

【报告】

    直接暴力题。不过最后还是用了记忆化搜索。总感觉直接暴力会TLE。网上有大神用线段树做的,ORZ...

【程序】

// Task: 1032 The 3n + 1 problem
// Designer: Rsky 2013/09/01
#include
#include
#include
#include
#include
using namespace std;
map hash;
inline int calc(int x)
{
    if (hash.find(x)!=hash.end()) return hash[x];
    else if (x%2==0) return hash[x]=calc(x/2)+1;
    else return hash[x]=calc(3*x+1)+1;
}
int main()
{
    int x,y;
    hash.clear();
    hash[1]=1;
    while (scanf("%d%d",&x,&y)!=EOF)
    {
        int ans=0;
        for (int i=x;i<=y;i++)
             ans=max(ans,calc(i));
        for (int i=y;i<=x;i++)
             ans=max(ans,calc(i));
        printf("%d %d %d\n",x,y,ans);
    }
    return 0;
}

 

posted @ 2013-09-01 21:01  为美好世界献上珂学  阅读(93)  评论(0)    收藏  举报