【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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值