Friday, July 25, 2014

UVa - 371 - Ackermann Functions

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    long long L, H, x, y, i, temp, cnt, ans, mx;
    while(cin >> x >> y){
        if(x == 0 && y == 0)break;
        L = min(x, y);
        H = max(x, y);
        mx = 0;
        if(L == 1)mx = 3, ans = 1;
        for(i = L; i <= H; i++){
            temp = i, cnt = 0;
            while(temp > 1){
                if(temp%2 == 0)temp /= 2, cnt++;
                else temp = (3*temp) + 1, cnt++;
            }
            if(cnt > mx)mx = cnt, ans = i;
        }
        cout << "Between " << L << " and " << H << ", " << ans << " generates the longest sequence of " << mx <<" values." << endl;
     }
    return 0;
}

No comments:

Post a Comment