ProgrammingHardCompetitive coding (CP)
How many subsets of {1, 2, 3, 4, 5} sum to exactly 10? Compute with this program.
How many subsets of {1, 2, 3, 4, 5} sum to exactly 10? Compute with this program.
#include <stdio.h>
int main(void) {
int c = 0;
for (unsigned m = 0; m < 32; m++) {
int s = 0;
for (int i = 0; i < 5; i++)
if (m >> i & 1u) s += i + 1;
if (s == 10) c++;
}
printf("%d", c);
}- A. 4
- B. 2
- C. 5
- D. 3
Correct answer: D. 3
The subsets with sum 10 are {1, 2, 3, 4}, {2, 3, 5} and {1, 4, 5}, so the count is 3.
More C Programming questions
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { int x = (const int )a, y = (const int )b…
- What is printed? c #include <stdio.h> int main(void) { unsigned s = 0; for (unsigned b = 1; b; b <<= 1) s += 1; printf("%u", s); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (3 & 1) == 1 ? 'y' : 'n'); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (7 & 5) | (2 ^ 6) << 1); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 5 > 3 ? 2 > 1 ? 7 : 8 : 9); }
- What is printed? c #include <stdlib.h> #include <stdio.h> int cmp(const void a, const void b) { int x = (const int )a, y = (const int )b…