水一道入门题,纯属消遣,代码略丑,没怎么优化
#include <iostream>
typedef unsigned int unit;
using std::cin;
using std::cout;
using std::endl;
unit Cycle(unit x)
{
unit count = 1;
while (x != 1)
{
if (x & 1) x += x * 2 + 1;
else x >>= 1;
++count;
}
return count;
}
int main()
{
unit m, n, fst,max;
while (cin>>m>>n)
{
cout << m << " " << n <<" "; //先输出的话可以减少一个表示状态的变量,就不用考虑m,n的大小分别输出了。
max = 0;
if (m > n)
{
m ^= n;
n ^= m;
m ^= n;
}
for (fst = m; fst <= n; ++fst)
{
if (max < Cycle(fst)) max = Cycle(fst);
}
cout << max << endl;
}
return 0;
}