奋斗吧少年


私信TA

用户名:ifighting

访问量:807

签 名:

等  级
排  名 9946
经  验 1064
参赛次数 2
文章发表 1
年  龄 0
在职情况 学生
学  校 江苏
专  业

  自我简介:

日常敲敲代码,有益身心健康

TA的其他文章

解题思路:
这个问题其实很简单,无非就是先把输入的数据先存储起来,在对每组数据进行一个找最大周期长度的过程,存储的话我用的是vector,找最大周期长度写一个while循环
注意事项:
1、算最大周期长度要把原数和1也算进去

2、输入的一组数很可能不是按从小到大的顺序,所以要先排一下,这点可能是大部分人都会忽视的一点(一开始我就是这样哈哈)
参考代码:

#include<iostream>

#include<vector>

using namespace std;

int  main()

{

    //输入n对数

int a,b;

int temp;

//max表示每个数的最大周期长度,cmax表示这组数中的最大周期长度

int max=1,cmax=1;

//存储输入数据

vector<int> num1;

vector<int> num2;

while(cin>>a>>b)

{

num1.push_back(a);

        num2.push_back(b);

}

    vector<int>::iterator it1;

vector<int>::iterator it2;

for(it1=num1.begin(),it2=num2.begin();it1!=num1.end(),it2!=num2.end();it1++,it2++)

{

cout<<*it1<<" "<<*it2<<" ";

a=*it1;

b=*it2;

//确保这组数按从小到大排序

if(a>b)

{

int t=a;

a=b;

b=t;

}

//核心部分:寻找一组数之中的最大周期长度

        for(int i=a;i<=b;i++)

{

if(i<1||i>=1000000) continue;

temp=i;

            while(temp!=1)

{

              if(temp%2==0)

  {

                 temp=temp/2;

                 max++;

  }

  else

  {

  temp=3*temp+1;

  max++;

  } 

}

if(i==a) cmax=max;

if(max>cmax) cmax=max;

max=1;

}

cout<<cmax<<endl;

cmax=1;

}

return 0;

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区