Question:ZJ c039. 00100 - The 3n + 1 problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include<bits/stdc++.h> using namespace std;
int main(){ int ni, nj, cot=0, maxnum=0, tmpni, tmpnj; while(cin >> ni >> nj){ tmpni = ni, tmpnj = nj; if(tmpni > tmpnj) swap(tmpni, tmpnj);
for(int i=tmpni; i<=tmpnj ; i++){ cot = 0; int n=i; while(1){ cot++; if(n == 1) break; else if(n%2 == 1) n = (3*n)+1; else n/=2; } if(cot > maxnum) maxnum = cot; }
cout << ni << " " << nj << " " << maxnum << endl; } }
|