#include #include #include using namespace std; int ptr = 0; struct comb { int num; int iter; }; int compare(const void * a, const void * b) { return( (int)(*(struct comb *)a).num - (int)(*(struct comb *)b).num); } int compareMyType(const void * a, const void * b) { if( (int)(*(struct comb*)a).num > (int)(*(struct comb*)b).num) return 1; if( (int)(*(struct comb*)a).num == (int)(*(struct comb*)b).num) return 0; if( (int)(*(struct comb*)a).num < (int)(*(struct comb*)b).num) return -1; return 0; //never reached } void next(struct comb& in) { if((in.num % 2) == 0) { in.num = in.num / 2; } else { in.num = 3 * in.num + 1; } in.iter++; } int main() { int x1; int x2; int vysl1; //int vysl2; int orig1; int orig2; struct comb mnozina[1024]; int swap = 0; struct comb * cptr; int comp1; int comp2; while(1) { ptr = 0; struct comb test; test.iter = 0; scanf("%d %d", &x1, &x2); orig1 = x1; orig2 = x2; if((x1 == 0) && (x2 == 0)) { break; } if((x1 % 2) == 0) { comp1 = x1 / 2; } else { comp1 = x1 * 3 + 1; } if((x2 % 2) == 0) { comp2 = x2 / 2; } else { comp2 = x2 * 3 + 1; } if(comp1 < comp2) { test.num = x1; } else { test.num = x2; swap = 1; } while(test.num != 1) { mnozina[ptr] = test; ptr++; next(test); } if(swap == 1) { test.num = x1; } else { test.num = x2; } test.iter = 0; qsort(mnozina, ptr, sizeof(struct comb), compare); for(int i = 0; i < ptr; i++) { printf("%d ", ((struct comb) mnozina[i]).num); } while(1) { cptr = (struct comb *) bsearch((void *)&test.num, (void *)mnozina, ptr, sizeof(struct comb), compareMyType); if(cptr != NULL) { vysl1 = (int)(*(struct comb *)cptr).iter; goto end; } next(test); } end: if(swap == 1) { printf("%d needs %d steps, %d needs %d steps, they meet at %d\n", x1, test.iter, x2, vysl1, test.num); } else { printf("%d needs %d steps, %d needs %d steps, they meet at %d\n", x1, vysl1, x2, test.iter, test.num); } } return 0; }