/* * collatz.C */ #include #include typedef unsigned int NUMBER; using namespace std; 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; while (true) { aMap[state.a] = counter; bMap[state.b] = counter; map::iterator 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; } 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; } state.a = step(state.a); state.b = step(state.b); counter++; } } return 0; }