ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
#include <stdio.h>
int total = 0, c1 = 0;
void hanoi(int n, char a, char b, char c) {
if (n == 0) return;
hanoi(n - 1, a, c, b);
total++;
if (n == 1) c1++;
hanoi(n - 1, c, b, a);
}
int main(void) {
hanoi(5, 'A', 'B', 'C');
printf("%d %d", total, c1);
}- A. 31 15
- B. 32 16
- C. 30 16
- D. 31 16
Correct answer: D. 31 16
Towers of Hanoi with n disks takes 2^n - 1 moves, so 31 for n = 5. Disk 1 moves on every other move, giving 2^(n-1) = 16 times.
More C Programming questions
- What is printed? c #include <stdio.h> int bit[9] = {0}; void add(int i, int v) { for (; i <= 8; i += i & -i) bit[i] += v; } int query(int…
- Assume int is 32 bits. What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint8t b = 0xF3; int e = (int)(b ^…
- What is printed? c #include <stdint.h> #include <stdio.h> int tz(uint32t x) { int n = 0; while (!(x & 1u)) { x >>= 1; n++; } return n; }…
- Assume int is 32 bits with limits INTMIN = -2147483648 and INTMAX = 2147483647. What is printed? c #include <limits.h> #include <stdio.h>…
- int n = 8, c = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j = 2) c++; printf("%d", c); What is printed?
- int c = 0; for (int i = 1; i <= 100; i = 3) c++; printf("%d", c); What is printed?