#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]; int lastIndexA = 0; int lastIndexB = 0; while(A != 0){ arrayA[0] = A; arrayB[0] = B; int number = 0; for (int i = 0; i < MAX_LENGTH-1; i++){ if(lastIndexA == 0){ number = getNext(arrayA[i]); arrayA[i+1] = number; if(number == 1){ lastIndexA = (i+1); } } if(lastIndexB == 0){ number = getNext(arrayB[i]); arrayB[i+1] = number; if(number == 1){ lastIndexB = (i+1); } } if(lastIndexA != 0 && lastIndexB != 0){ i = MAX_LENGTH; } } for (int i = 0; i < lastIndexB; i++){ for (int j = 0; j < lastIndexA; j++){ if(arrayB[i] == arrayA[j]){ reachedNumber = arrayB[i]; stepsA = j; stepsB = i; j = MAX_LENGTH; i = MAX_LENGTH; } if(arrayA[i] == arrayB[j]){ reachedNumber = arrayB[j]; stepsA = i; stepsB = j; j = MAX_LENGTH; i = MAX_LENGTH; } } } lastIndexA = 0; lastIndexB = 0; 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; }