ProgrammingVery hardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
void gen(int d, int v) {
if (d == 0) {
printf("%d ", v);
return;
}
gen(d - 1, v * 2 + 1);
gen(d - 1, v * 2);
}
int main(void) {
gen(3, 0);
}- A. 0 1 2 3 4 5 6 7
- B. 7 6 5 4 3 2 1 0
- C. 3 2 1 0 7 6 5 4
- D. 7 5 3 1 6 4 2 0
Correct answer: B. 7 6 5 4 3 2 1 0
Each call visits the odd child before the even child, so the leaves come out in decreasing order from 7 down to 0. Swapping the two calls would print the values in increasing order.
More C Programming questions
- What is printed? c #include <stdio.h> unsigned inv(unsigned g) { unsigned n = 0; for (; g; g >>= 1) n ^= g; return n; } int main(void) {…
- What is printed? c #include <stdio.h> int main(void) { int dp[16] = {0}; int sum = 0; for (int i = 1; i < 16; i++) dp[i] = dp[i >> 1] + (i…
- Assume int is 4 bytes with 4-byte alignment and char is 1 byte. What is printed? c #include <stdio.h> struct A { char c; int i; char d; }…
- How many subsets of {1, 2, 3, 4} (the empty set included) have an XOR of zero? c #include <stdio.h> int main(void) { int count = 0; for…
- What is printed? Assume int is 32 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint8t b = (uint8t)-200; printf("%d"…
- What is printed? c #include <stdio.h> int main(void) { char s[] = "abcdef"; char p = s; int n = 0; while (p && p - s < 4) n += p++ - 'a'…