题目的意思是把输入的i,j 从i到j的每一个数 做循环,输出循环次数最大的值

易错的地方:做循环是容易直接用i进行计算 i=i/2;或i=i*3+1; 这样i的值改变就不能在做下面数的循环

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
  int i,j,t,max,cnt,n,a;
  while(cin>>i>>j)
  {
    cout<<i<<' '<<j<<' ';
    max=-100;
    if(i>j)
    {
      t=j;
      j=i;
      i=t;
    }
    for(a=i;a<=j;a++)
    {
      n=a; //注意
      cnt=1;
      while(n-1)
      {
        if(n%2==0)
          n=n/2;
        else
          n=3*n+1;
        cnt++;

      }
      if(cnt>max)
        max=cnt;
    }
    cout<<max<<endl;
  }
  
  return 0;
}

 

posted on 2016-09-26 19:20  zhangxiaofanfan  阅读(160)  评论(0编辑  收藏  举报