ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int moves = 0;
void h(int n, char from, char via, char to) {
if (n == 0) return;
h(n - 1, from, to, via);
if (++moves == 5) printf("%d:%c->%c ", n, from, to);
h(n - 1, via, from, to);
}
int main(void) {
h(3, 'A', 'B', 'C');
printf("%d", moves);
}- A. 2:B->C 7
- B. 1:A->C 7
- C. 1:B->A 7
- D. 3:A->C 8
Correct answer: C. 1:B->A 7
The 3-disk solution is: 1 A→C, 2 A→B, 1 C→B, 3 A→C, 1 B→A, 2 B→C, 1 A→C. Move 4 is the big disk; move 5 starts the second half, h(2, 'B', 'A', 'C'), whose first step moves disk 1 from B to its 'via' peg A. In total 2^3 − 1 = 7 moves are made.
More C Programming questions
- What is printed? c for (unsigned n = 5; n < 8; n++) printf("%u ", n ^ (n >> 1)); unsigned g = 13, b = 0; for (; g; g >>= 1) b ^= g…
- What is printed? c int walk(int n) { static int calls = 0; calls++; if (n > 1) { walk(n / 2); walk(n / 3); } return calls; } int…
- What is printed? c int calls = 0; int A(int m, int n) { calls++; if (m == 0) return n + 1; if (n == 0) return A(m - 1, 1); return A(m - 1…
- What is printed? c int a[7] = {1, 2, 3, 4, 5, 6, 7}, n = 7, i = 0; while (n > 1) { i = (i + 2) % n; printf("%d ", a[i]); for (int j = i; j…
- What is printed? c / LP64: 4-byte int, 8-byte pointers / void f(int m[][4], int n) { printf("%zu %zu %td ", sizeof m, sizeof m[0], (char…
- What is printed? c int a = 1, b = 2, c = 3; a += b -= c = 2; int d = (a, b) > c ? a-- : c++; printf("%d %d %d %d", a, b, c, d);