#include using namespace std; int getNext(int n){ if(n%2 == 0){ return n/2; } else{ return (3*n +1); } } int main(){ int A,B; const int MAX_LENGTH = 1000000; //int lenghtWithOne = 0; int stepsA = 0; int stepsB = 0; cin >> A; cin >> B; int reachedNumber = 0; int arrayA[MAX_LENGTH]; int arrayB[MAX_LENGTH]; while(A != 0){ arrayA[0] = A; arrayB[0] = B; for (int i = 0; i < MAX_LENGTH-1; i++){ arrayA[i+1] = getNext(arrayA[i]); arrayB[i+1] = getNext(arrayB[i]); } for (int i = 0; i < MAX_LENGTH; i++){ for (int j = 0; j < MAX_LENGTH; j++){ if(arrayB[i] == arrayA[j]){ reachedNumber = arrayB[i]; stepsA = j; stepsB = i; j = MAX_LENGTH; i = MAX_LENGTH; } } } cout << A << " needs " << stepsA; cout <<" steps, " << B; cout <<" needs " << stepsB; cout << " steps, they meet at " << reachedNumber << "\n"; //after all cin >> A; cin >> B; } return 0; }