注解
1、模拟。
2、当输入的a比b大时,要先交换a与b。
代码
#include <iostream>
#include <algorithm>
using namespace std;
int f(int a, int b) {
if(a>b) {
swap(a, b);
}
int max = -1;
for(int i=a; i<=b; i++) {
int count = 1;
int temp = i;
while(temp!=1) {
if(temp%2==1) {
temp = temp * 3 + 1;
} else {
temp = temp / 2;
}
count++;
}
if(count>max) {
max = count;
}
}
return max;
}
int main() {
int a, b;
while(cin>>a>>b) {
int maxn = f(a, b);
cout<<a<<" "<<b<<" "<<maxn<<endl;
}
return 0;
}