ProgrammingVery hardCompetitive coding (CP)
How many ordered ways are there to write 10 as a sum of 1s, 2s and 3s? Compute with this program.
How many ordered ways are there to write 10 as a sum of 1s, 2s and 3s? Compute with this program.
#include <stdio.h>
int f(int n) {
if (n == 0) return 1;
if (n < 0) return 0;
return f(n - 1) + f(n - 2) + f(n - 3);
}
int main(void) {
printf("%d", f(10));
}- A. 256
- B. 181
- C. 200
- D. 274
Correct answer: D. 274
Each ordered sum is counted once by its last part, which is 1, 2 or 3, so f(n) = f(n-1) + f(n-2) + f(n-3). The sequence gives f(10) = 274.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { unsigned n = 44; while (n & (n - 1)) n &= n - 1; printf("%u", n); }
- What is printed? c #include <stdio.h> int main(void) { unsigned n = 0; printf("%d", (n & (n - 1)) == 0); }
- What is printed? Assume uint32t is 32 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint32t h = 2166136261u; h ^=…
- What is printed? c #include <stdio.h> #include <stdlib.h> #include <string.h> struct Rec { char name[4]; int score; }; int cmp(const void…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 6; printf("%u", (x >> 1) ^ x); }
- What is printed? c #include <stdio.h> int main(void) { int x = 7; printf("%d", (x << 3) + x); }