//这题贡献了一次WA,原因是由于没有考虑到输入的两个数中,有可能后面的数大于前面的数 !
#include "iostream"
using namespace std;
int solve(int num)
{
int i, c = 1;
if (num == 1) return c;
else
{
while (num != 1)
{
if (num % 2)
{
num = num * 3 + 1;
c++;
}
else
{
num = num / 2;
c++;
}
}
return c;
}
}
int main()
{
int a, b, maxc, i, temp, ansa, ansb;
while (cin >> a >> b)
{
maxc = 0;
ansa = a, ansb = b;
if (a > b)//作判断,如果后一个数大于前一个数,就交换位置!
{
temp = a;
a = b;
b = temp;
}
for (i = a ; i <= b; i++)
{
temp = solve(i);
if (temp > maxc)
maxc = temp;
}
cout << ansa << " " << ansb << " " << maxc << endl;
}
system("pause");
}
/*
1 10
100 200
201 210
900 1000
10000 1
500 200
200 200
1 1
*/
poj 1207 The 3n + 1 problem
最新推荐文章于 2025-08-18 20:48:49 发布