ProgrammingHardProduct company OA
What is printed?
What is printed?
int g[3][3] = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};
int mp(int r, int c) {
if (r == 0 && c == 0) return g[0][0];
if (r < 0 || c < 0) return 1000;
int a = mp(r - 1, c), b = mp(r, c - 1);
return g[r][c] + (a < b ? a : b);
}
int main(void) { printf("%d", mp(2, 2)); }- A. 6
- B. 12
- C. 8
- D. 7
Correct answer: D. 7
mp(r, c) is the cheapest sum along a path from the top-left that moves only right or down; 1000 acts as infinity for cells outside the grid. The best path is 1 → 3 → 1 → 1 → 1 = 7. Going straight down the left and along the bottom costs 1 + 1 + 4 + 2 + 1 = 9.
More C Programming questions
- What is printed? c unsigned next(unsigned x) { unsigned c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; } int main(void) {…
- What is printed? c const char s = "abcabcab"; const char p = strstr(s, "cab"); printf("%td %s %td", p - s, p + 3, strstr(p + 1, "cab") - s);
- What is printed? c struct A { int v[3]; }; struct A make(void) { struct A a = {{1, 2, 3}}; return a; } int main(void) { struct A x =…
- What is printed? c const char s = "a1b2c3d4"; int sum = 0; for (; s; s++) { if (s >= 'a') continue; if (s == '3') break; sum += s - '0'; }…
- What is printed? c int s = 0; for (int i = 0; i < 5; i++) { switch (i) { case 1: continue; case 3: break; default: s += 10; } s += i; }…
- What is printed? c int a[] = {10, 20, 30}; printf("%d", a[1, 2]);