ProgrammingHardProduct company OA
What is printed?
What is printed?
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;
return ways(amt - c[k - 1], k) + ways(amt, k - 1);
}
int main(void) { printf("%d", ways(10, 3)); }- A. 8
- B. 12
- C. 10
- D. 88
Correct answer: C. 10
Each step either uses the largest allowed coin again or drops it for good, so combinations are counted once regardless of order. With 0, 1 or 2 fives the remainders 10, 5 and 0 can be made from 1s and 2s in 6, 3 and 1 ways: 10 in total. Counting orders would give far more.
More C Programming questions
- 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…
- For ints a, b >= 0, which test correctly tells, before adding, whether a + b would overflow?
- What is printed? c const char s = " hi there you "; int words = 0, in = 0; for (; s; s++) { if (s == ' ') in = 0; else if (!in) { in = 1…
- What is printed? c int sub(const char a, const char b) { / is a a subsequence of b? / if (!a) return 1; if (!b) return 0; return sub(a ==…