371 akermann.cpp

来自「UVA solutions to few selected problems」· C++ 代码 · 共 49 行

CPP
49
字号
#include<iostream>
using namespace std;
int main()
{
    long long i,j,max,t,l,change,req,start,end;
    while(cin>>i>>j)
    {               
                    start=i;
                    end=j;
                    max=0;
                    t=1;
                    if(i==0 && j==0)
                    break;
                    if(i>j)
                    {
                           change=i;
                           i=j;
                           j=change;
                    }
                    while(j>=i)
                    {
                            l=i;
                            t=1;
                            do
                            {          
                                       if(i%2==0)
                                       i=i/2;
                                       else 
                                       i=3*i+1;
                                       t++;
                            }while(i!=1);
                            if(t>max)
                            {
                            req=l;         
                            max=t;
                            }
                            l++;
                            i=l;
                    }   
                    if(start<=end)
                    cout<<"Between "<<start<<" and "<<end<<", "<<req<<" generates the longest sequence of "<<(max-1)<<" values."<<endl;
                    if(start>end)
                    cout<<"Between "<<end<<" and "<<start<<", "<<req<<" generates the longest sequence of "<<(max-1)<<" values."<<endl;
                    
    }
    system("pause") ;
}
                                   

⌨️ 快捷键说明

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