ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int p(int n, int m) { /* ways to write n as a sum of parts <= m */
if (n == 0) return 1;
if (n < 0 || m == 0) return 0;
return p(n - m, m) + p(n, m - 1);
}
int main(void) { printf("%d %d", p(5, 5), p(6, 2)); }- A. 16 4
- B. 7 3
- C. 7 4
- D. 5 4
Correct answer: C. 7 4
The recursion either uses a part of size m or never uses m again, so it counts unordered sums. 5 has 7 partitions (5, 4+1, 3+2, 3+1+1, 2+2+1, 2+1+1+1, 1+1+1+1+1). With parts 1 and 2 only, 6 can use zero to three 2s: 4 ways. '16' counts ordered sums.
More C Programming questions
- What is printed? c printf("%#x %#o %x", 255, 8, -1 & 0xFF);
- Assume 8-bit unsigned char. What is printed? c printf("%d %d %d", (int)-3.7, (unsigned char)-1, (int)(0.1 30));
- What is printed? c int ways(int amt, int k) { static const int c[] = {1, 2, 5}; if (amt == 0) return 1; if (amt < 0 || k == 0) return 0…
- On an LP64 system (8-byte pointers, 4-byte int), what is printed? c int a[10]; printf("%zu %zu %zu", sizeof(&a), sizeof(&a), sizeof(a + 0));
- What is printed? c int i = 0, j = 0; if (i++ & j++) ; if (i++ && j++) ; printf("%d %d", i, j);
- What is printed? c int a[6] = {4, 8, 15, 16, 23, 0}; int find(int x) { a[5] = x; int i = 0; while (a[i] != x) i++; return i < 5 ? i : -1…