- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- int gen(int n){
- int count = 0;
- if(n == 1)
- return count = 1;
- if(n % 2 == 0){
- return count = 1 + gen(n / 2);
- }
- else{
- return count = 1 + gen(3 * n + 1);
- }
- }
- int main(){
- int m, n, x, y;
- while(cin >> x >> y){
- cout << x << " " << y << " ";
- m = max(x, y);
- n = min(x, y);
- int mx = 0;
- for(int i = n; i <= m; i++){
- gen(i);
- mx = max(mx,gen(i));
- }
- cout << mx << endl;
- }
- return 0;
- }
Wednesday, July 2, 2014
UVa - 100 - The 3n + 1 problem - recursive solution
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment