本题比较基础,依次计算每个数的cycle length,输出其中最大的即可。
注意输入的 i 不一定大于 j,一定要事先判断,但输出时要按照输入的顺序,所以要先输出 i 和 j。
#include <iostream>
using namespace std;
int main()
{
int a, b;
int num, max;
int temp;
while (cin >> a >> b)
{
cout << a << " " << b << " ";
if (a > b)
{
temp = a;
a = b;
b = temp;
}
max = 0;
for (int i = a; i <= b; i++)
{
num = 0;
temp = i;
while (temp != 1)
{
++num;
if (temp % 2 == 1)
temp = temp * 3 + 1;
else
temp = temp / 2;
}
++num;
if (num > max)
max = num;
}
cout << max << endl;
}
return 0;
}
继续加油。