Analysis of past CSP-J preliminary test questions | 2020 CSP-J preliminary test reading program (28-33)

Learn C++ from a young age! Record the questions in the CSP-J exam preparation study process and record every moment.

Attached is a summary post: Analysis of past CSP-J preliminary test questions | Summary_csp past past questions_Blog of a communicator who loves programming-CSDN blog


#include <algorithm>
#include <iostream>
using namespace std;

int n;
int d[50][2];
int ans;

void dfs(int n, int sum) {
    if (n == 1) { 
        ans = max(sum, ans);  //希望把n个合并到只剩一个时代价能够最大
        return;
    }
    for (int i = 1; i < n; ++i) {
        int a = d[i - 1][0], b = d[i - 1][1];
        int x = d[i][0], y = d[i][1];
        d[i - 1][0] = a + x;  //枚举相邻的两个元素合并
        d[i - 1][1] = b + y;
        for (int j = i; j < n - 1; ++j)  //抹去第i个元素
            d[j][0] = d[j + 1][0], d[j][1] = d[j + 1][1];
        int s = a + x + abs(b - y);  //合并相邻两个元素的代价
        dfs(n - 1, sum + s);  //回溯法
        for (int j = n - 1; j > i; --j)  //还原第i个元素
            d[j][0] = d[j - 1][0], d[j][1] = d[j - 1][1];
        d[i - 1][0] = a, d[i - 1][1] = b;
        d[i][0] = x, d[i][1] = y;
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> d[i][0];
    for (int i = 0; i < n; ++i)
        cin >> d[i][1];
    ans = 0;
    dfs(n, 0);
    cout << ans << endl;
    return 0;
}

Assume that the input n is a positive integer not exceeding 50, and d[i][0] and d[i][1] are both positive integers not exceeding 10000. Complete the following true-false and multiple-choice questions:

28. If n is 0, the program may loop endlessly or run into an error. ( )

[Answer]: Wrong

【Analysis】

When n=0, the entire loop will not be executed and no error will occur.

29. If the input n is 20 and the next inputs are all 0, the output will be 0. ( )

【Answer】:

【Analysis】

When the input is all 0, s is 0, sum is also 0, so ans is also 0

30. The output number must not be less than either of the input d[i][0] and d[i][1]. ( wrong)

[Answer]: Wrong

【Analysis】

When n=1, there is no merging and ans is 0. At this time, it does not matter what the value in the d array is. ans will be less than or equal to the number in the d array, so it is wrong.

31. If the input n is 20, and the next input is 20 9s and 20 0s, the output is (A).

A.1890

B.1881

C.1908

D.1917

[Answer]: B

【Analysis】

The cost of the first merge is 18, the cost of the second merge is 27, the cost of the third merge is 36, ...., and the cost of the last merge is 9*20=180.

So the total cost=9*2+9*3+9*4+...+9*20=1881

32. If the input n is 30, and the next input is 30 0s and 30 5s, the output is ( ).

A.2000

B.2010

C.2030

D.2020

[Answer]: C

【Analysis】

The cost of the first merge is 0, the cost of the second merge is 5, the cost of the third merge is 10,..., and the cost of the last merge is 28*5.

All total costs=0+5*1+5*2+...+5*28=2030

33. If the input n is 15, and the next input is 15 to 1, and 15 to 1, the output is (C).

A.2440

B.2220

C.2240

D.2420

[Answer]: C

【Analysis】

The cost of the first merge is 30, the cost of the second merge is 58, the cost of the third merge is 84, the cost of the fourth merge is 108, the cost of the fifth merge is 130, and the cost of the sixth merge is The cost of the seventh merger is 168, the cost of the eighth merger is 184, the cost of the ninth merger is 198, the cost of the tenth merger is 210, the cost of the eleventh merger is 220, The cost of the twelfth merger is 228, the cost of the thirteenth merger is 234, and the cost of the fourteenth merger is 238.

The total cost = 30+58+84+108+...+238=2240, choose C

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132887848