ProgrammingHardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
int main(void) {
int a[] = {1, 2, 3}, cnt[3], total = 0;
for (int i = 0; i < 3; i++) {
cnt[i] = 1;
for (int j = 0; j < i; j++)
if (a[j] < a[i]) cnt[i] += cnt[j];
total += cnt[i];
}
printf("%d", total);
}- A. 6
- B. 8
- C. 4
- D. 7
Correct answer: D. 7
cnt[i] counts the increasing subsequences that end at a[i], giving 1, 2 and 4. Their sum, 7, is the number of non-empty increasing subsequences.
More C Programming questions
- How many binary strings of length 5 contain no two adjacent 1s? Compute with this recursive program. c #include <stdio.h> int f(int n) {…
- What is printed? c #include <stdio.h> struct item { int w, v; }; int main(void) { struct item items[] = {{1, 10}, {2, 20}, {3, 30}}…
- What is printed? c #include <stdio.h> int main(void) { const char s = "hello"; printf("%c%c", s[1], (s + 4)); }
- What is printed? c #include <stdio.h> int main(void) { int x = 1, y = 2; const int cp = &x; int const pc = &y; pc = 5; printf("%d %d", cp…
- What is printed? Assume double is 8 bytes. c #include <stdio.h> int main(void) { printf("%zu", sizeof(1 ? 'a' : 1.0)); }
- What is printed? c #include <stdio.h> #include <stdlib.h> #include <string.h> int cmp(const void a, const void b) { return strcmp((const…