1032 the 3n + 1 problem.cpp

来自「威士忌的HDU题解.大概有260多题的源码。对于学习非常有好处。」· C++ 代码 · 共 50 行

CPP
50
字号
/*
1032 The 3n + 1 problem
Time Limit : 1000 ms  Memory Limit : 32768 K  Output Limit : 5120 K

GUN C++
*/
//穷举
#include <iostream.h>
using namespace std;

const int nMax=1000000;

unsigned short rec[nMax+1]={0};

int main()
{
    int i,j,ca,temp,max,count;
    while(cin>>i>>j)
    {
        cout<<i<<" "<<j<<" ";
        if(i>j)
        {   temp=i;i=j;j=temp;}
        max=0;
        for(ca=i;ca<=j;ca++)
        {
            if(rec[ca]!=0)
            {   count=rec[ca];}
            else
            {
                temp=ca;count=0;
                while(temp!=1)
                {
                    if(temp%2==1)
                    {   temp=temp*3+1;}
                    else
                        temp/=2;
                    count++;
                }
                rec[ca]=count;
            }
            if(count>max)
                max=count;
        }
        max++;
        cout<<max<<endl;
    }
    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?