ProgrammingVery hardCompetitive coding (CP)
How many ordered ways are there to write 6 as a sum of parts 1, 2 and 3? Compute with this program.
How many ordered ways are there to write 6 as a sum of parts 1, 2 and 3? 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(6));
}- A. 21
- B. 24
- C. 26
- D. 32
Correct answer: B. 24
f(n) = f(n - 1) + f(n - 2) + f(n - 3) with f(0) = 1 counts ordered sums by their last part, and it gives 1, 1, 2, 4, 7, 13, 24 for n = 0 to 6.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 7; int i = 0; while (x & 1u) { x >>= 1; i++; } printf("%d", i); }
- What is printed? Assume unsigned short is 16 bits. c #include <stdio.h> int main(void) { unsigned short a = 255, b = 255; unsigned c = a…
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { char w[] = {"ab", "cde"}; char p = w; printf("%d"…
- What is printed? c #include <stdio.h> int main(void) { int a[5] = {0}; int p = a + 4; printf("%ld", (long)(p - &a[0])); }
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 3, 2, 5}, c = 0; for (int p = a; p < a + 4; p++) if (p > 2) c++…
- What is printed? c #include <stdio.h> int main(void) { int x = 0, p = &x; p++ = 5; printf("%d", x); }