/* * collatz.C */ #include #include using namespace std; typedef unsigned int NUMBER; typedef map IMap; typedef map > SMap; typedef struct { int a; int b; inline bool isEnd() { return (a == 0) && (b == 0); } } TestCase; TestCase readTestCase() { TestCase result; scanf("%d", &result.a); scanf("%d", &result.b); return result; } int step(int value) { if ((value % 2) == 0) return value >> 1; else return (3 * value) + 1; } int main() { TestCase tCase; while (!(tCase = readTestCase()).isEnd()) { TestCase state = tCase; map aMap; map bMap; int counter = 0; bool aRoot = true,bRoot = true; while (true) { aMap[state.a] = counter; bMap[state.b] = counter; map::iterator it; if(bRoot) { it = aMap.find(state.b); if (it != aMap.end()) { printf("%d needs %d steps, %d needs %d steps, they meet at %d\n", tCase.a, it->second, tCase.b, counter, state.b); break; } } if(aRoot) { it = bMap.find(state.a); if (it != bMap.end()) { printf("%d needs %d steps, %d needs %d steps, they meet at %d\n", tCase.a, counter, tCase.b, it->second, state.a); break; } } if(aRoot) { state.a = step(state.a); } if(bRoot) { state.b = step(state.b); } if(state.a == 1) { aRoot = false;} if(state.b == 1) { bRoot = false;} counter++; } } return 0; }