ProgrammingHardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
int t[256];
int main(void) {
for (int i = 1; i < 256; i++) t[i] = t[i >> 1] + (i & 1);
printf("%d %d", t[200], t[255]);
}- A. 4 8
- B. 3 7
- C. 2 8
- D. 3 8
Correct answer: D. 3 8
t[i] stores the popcount of i, built from the popcount of i >> 1 plus the low bit. 200 = 11001000 has three set bits, and 255 has eight.
More C Programming questions
- What is printed? Assume uint8t is 8 bits and unsigned int is 32 bits. c #include <stdint.h> #include <stdio.h> uint8t sat(uint8t a, uint8t…
- What is printed? c #include <stddef.h> #include <stdio.h> int main(void) { sizet a = 3, b = 5; printf("%d", (a - b) / 2 > 0); }
- What is printed? c #include <stdio.h> int od(unsigned n); int ev(unsigned n) { return n == 0 ? 1 : od(n - 1); } int od(unsigned n) {…
- A complete binary tree of 7 nodes is stored in an array with children of node i at 2i+1 and 2i+2. The values are v = {5, 3, 8, 1, 4, 9…
- What is printed? c #include <stdio.h> #include <stdlib.h> struct Pair { int a, b; }; int cmp(const void p, const void q) { const struct…
- What is printed? c #include <stdio.h> #include <stdlib.h> #include <string.h> int cmp(const void a, const void b) { const char x = (const…