ProgrammingHardCompetitive coding (CP)
What is printed?
What is printed?
#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 & 1);
for (int i = 0; i < 16; i++)
sum += dp[i];
printf("%d", sum);
}- A. 28
- B. 33
- C. 16
- D. 32
Correct answer: D. 32
dp[i] equals the number of set bits in i, and summing over 0 to 15 gives 32 (each of the 4 bit positions is set in 8 numbers). Summing only 1 to 15 gives the same total, so 33 is a plus-one slip.
More C Programming questions
- 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'…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { const int x = (int const )a; const int y =…
- What is printed? c #include <stdio.h> int main(void) { int m[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) m[i][j] = i 3…