#include #include #include int main(){ char * postupA = (char *)malloc(100000000); char * postupB = (char *)malloc(100000000); char * pole = (char *)malloc(100000000); int a, b; char *p_a, *p_b, *p_c; scanf("%d%d", &a, &b ); while (a != 0){ int countA = 0, countB = 0; int tmpA = a, tmpB = b; strcpy(postupA,""); strcpy(postupB,""); p_a = postupA; p_b = postupB; p_c = &pole[0]; if ( a == b ){ printf("%d needs 0 steps, %d needs 0 steps, they meet at %d\n", a, b, a); scanf("%d%d", &a, &b ); continue; } while ( tmpA != 1 ){ *p_c = tmpA; p_c++; if( tmpA % 2 == 0 ){ tmpA /= 2; *p_a = '0'; } else{ tmpA = 3 * tmpA + 1; *p_a = '1'; } p_a++; countA++; } while ( tmpB != 1 ){ if( tmpB % 2 == 0 ){ tmpB /= 2; *p_b = '0'; } else{ tmpB = 3 * tmpB + 1; *p_b = '1'; } p_b++; countB++; } while ( countA > 0 && countB > 0 ){ if ( postupA[countA - 1] == postupB[countB - 1] ){ countA--; countB--; p_c--; } else break; } printf("%d needs %d steps, %d needs %d steps, they meet at %d\n", a, countA, b, countB, *p_c); scanf("%d%d", &a, &b ); } return 0; }