⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1032 the 3n + 1 problem.cpp

📁 ACM 威士忌部分答案
💻 CPP
字号:
/*
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -