Analysis of C/C++ (Level 4) real questions in September 2021#中国电子学院#National Youth Software Programming Level Examination

insert image description here

Question 1: Optimal Path

A triangle of positive integer numbers looks like this:
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
There are many different paths from the top to the bottom of the triangle. For each path, add up the numbers on the path to get a sum, and the path with the largest sum is called the best path. Your task is to find the sum of the numbers on the best path.
Note: Each step on the path can only go from a number to the number on the next layer and its nearest bottom (directly below) or the number on the right (bottom right).
Time limit: 1000
Memory limit: 65536
Input
The first line is the triangle height 100>=h>=1, which is also the number of the bottom edge. Beginning with the second row, each row is the number of the corresponding row of the triangle, separated by a space.
Output
The length of the best path.
Sample input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample output
30

The following is the C language code implementation to solve the problem:

#include <stdio.h>

int maxPathSum(int triangle[][100], int N) {
    
    
    int dp[100][100] = {
    
    0};

    // 初始化最底层的dp值为三角形最底层的值
    for (int i = 0; i < N; i++) {
    
    
        dp[N - 1][i] = triangle[N - 1][i];
    }

    // 从倒数第二层开始迭代计算dp值
    for (int i = N - 2; i >= 0; i--) {
    
    
        for (int j = 0; j <= i; j++) {
    
    
            dp[i][j] = triangle[i][j] + (dp[i + 1][j] > dp[i + 1][j + 1] ? dp[i + 1][j] : dp[i + 1][j + 1]);
        }
    }

    return dp[0][0];
}

int main() {
    
    
    int N;
    scanf("%d", &N);

    int triangle[100][100];
    for (int i = 0; i < N; i++) {
    
    
        for (int j = 0; j <= i; j++) {
    
    
            scanf("%d", &triangle[i][j]);
        }
    }

    int maxSum = maxPathSum(triangle, N);
    printf("%d\n", maxSum);

    return 0;
}

The code uses the idea of ​​dynamic programming to solve the problem. Define a dp array, where dp[i][j] represents the sum of numbers of the best path from the top of the triangle to position (i, j).

Computes the dp array by iteration and returns dp[0][0] as the result.

First, initialize the bottom layer of the dp array to the value of the bottom layer of the triangle, that is, dp[N-1][i] = triangle[N-1][i].

Then, iteratively calculate the dp array from the penultimate layer. For position (i, j), the value of dp[i][j] can be calculated by:

  • dp[i][j] = triangle[i][j] + max(dp[i+1][j], dp[i+1][j+1]), that is, the value of the current position plus the next layer The sum of the numbers of the best paths for adjacent positions.

Finally, dp[0][0] is returned as the result, the sum of the numbers for the best path.

Topic 2: Number construction

Huohuobao intends to create an n-digit decimal number.
For each i from 1 to n, Huohuobao can choose one of the ki numbers 0-9 from xi,1, …, xi,ki as ai.
After the selection is over, a1a2…an forms a decimal number with n digits—this is the number created by the volcano treasure.
You need to help Huo Huo Bao calculate how many numbers he can create are multiples of 3.
Time limit: 1000
Memory limit: 65536
input
Input an integer n (1 ≤ n ≤ 18) in the first line, indicating the number of digits. In the next n lines, the first integer ki (1 ≤ ki ≤ 10) in each line indicates the number of candidate numbers in the i-th line. This is followed by ki two different numbers xi,1, …, xi,ki in the range 0-9. Enter to ensure that 0 is not the first optional item.
Output
You need to output an integer on a line, indicating the number of multiples of 3 among the numbers that Huobao can create.
Sample input
Sample input 1:
2
5 5 6 7 8 9
5 0 1 2 3 4
Sample input 2:
5
9 1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
10 0 1 2 3 4 5 6 7 8 9
10 0 1 2 3 4 5 6 7 8 9
10 0 1 2 3 4 5 6 7 8 9
sample output sample output
1:
9
Sample output 2:
30000
Prompt
The multiples of 3 that can be produced in sample 1 are 51, 54, 60, 63, 72, 81, 84, 90, 93.

To fix this, use backtracking to generate all possible numbers and check whether each number is a multiple of 3.

The following is the code implemented in C language:

#include <stdio.h>
#include <stdbool.h>

int count = 0;

bool isMultipleOfThree(int num) {
    
    
    return num % 3 == 0;
}

void generateNumbers(int digits, int candidates[][10], int currentDigit, int currentNumber) {
    
    
    if (currentDigit > digits) {
    
    
        if (isMultipleOfThree(currentNumber)) {
    
    
            count++;
        }
        return;
    }

    for (int i = 0; i < candidates[currentDigit][0]; i++) {
    
    
        generateNumbers(digits, candidates, currentDigit + 1, currentNumber * 10 + candidates[currentDigit][i + 1]);
    }
}

int main() {
    
    
    int n;
    scanf("%d", &n);

    int candidates[18][10];
    for (int i = 1; i <= n; i++) {
    
    
        scanf("%d", &candidates[i][0]);
        for (int j = 1; j <= candidates[i][0]; j++) {
    
    
            scanf("%d", &candidates[i][j]);
        }
    }

    generateNumbers(n, candidates, 1, 0);

    printf("%d\n", count);

    return 0;
}

The code generates all possible numbers recursively. For each digit, loop according to the number of candidate numbers, and form a new number with the current digit and the previous digit.

During the recursion, if the number of digits generated exceeds the given number of digits, check whether the number is a multiple of 3. If yes, increment the counter count by 1.

Finally, output the value of the counter count, that is, the number of multiples of 3 among the numbers created by the volcano.

Question 3: Cows for a walk

Starting from the center point of an infinite matrix, one step can only go right, up or left. How many ways are there to take exactly N steps without passing through the points that have already been walked?
Input
a number, representing N, N<=1000
output
How many schemes there are in the output digital analog 12345

Sample input
2
Sample output
7

To solve this problem, a dynamic programming method can be used to calculate the number of solutions under different synchronization numbers.

The following is the code implemented in C language:

#include <stdio.h>

int countWalks(int N) {
    
    
    int dp[2][3] = {
    
    0}; // 使用滚动数组保存状态

    dp[1][1] = 1; // 初始位置

    for (int step = 1; step <= N; step++) {
    
    
        int curr = step % 2; // 当前行的索引
        int prev = (step - 1) % 2; // 上一行的索引

        dp[curr][0] = (dp[prev][1] + dp[prev][2]) % 12345; // 向右走
        dp[curr][1] = (dp[prev][0] + dp[prev][2]) % 12345; // 向上走
        dp[curr][2] = (dp[prev][0] + dp[prev][1] + dp[prev][2]) % 12345; // 向左走
    }

    return dp[N % 2][0] + dp[N % 2][1] + dp[N % 2][2];
}

int main() {
    
    
    int N;
    scanf("%d", &N);

    int numWalks = countWalks(N);
    printf("%d\n", numWalks);

    return 0;
}

The code uses a rolling array to save the state, and uses dp[i][j] to represent the number of plans to take i steps to reach position j.

Initially, set dp[1][1] to 1, indicating the initial position.

Then, iteratively calculate the dp array from step 2. For each step, calculate the number of solutions for the current step based on the number of solutions for the previous step:

  • Go right: dp[curr][0] = (dp[prev][1] + dp[prev][2]) % 12345

  • Going up: dp[curr][1] = (dp[prev][0] + dp[prev][2]) % 12345

  • Go left: dp[curr][2] = (dp[prev][0] + dp[prev][1] + dp[prev][2]) % 12345

Finally, return dp[N % 2][0] + dp[N % 2][1] + dp[N % 2][2] as the result, that is, the number of solutions that take N steps.

Hope the above code can help you solve your problem!

Question 4: Eat Cheese

Jerry is going to steal Tom's cheese. All the cheeses lined up in a straight line, each with a different level of deliciousness. However, if there are two adjacent pieces of cheese being eaten by Jerry, Tom will find this and quickly catch Jerry. Of course, Jerry hopes that the sum of the deliciousness of the cheese that can be eaten without being discovered by Tom is the largest. Of course, he can also choose not to eat a piece of cheese. Please help him plan a plan for stealing, and tell him how much deliciousness he can steal at most.
Time limit: 1000
Memory limit: 65536
Input
The first line is an integer T (T<=100), indicating the number of test data sets. Next, each set of test data contains two rows. Among them, an integer n (1 <= n <= 100,000) in the first line indicates the number of cheeses; n integers in the second line indicate the deliciousness of the cheese on this line. Please note that the deliciousness is guaranteed to be int type storage, and may be negative.
Output
For each set of test data, output an integer representing the maximum sum of delicacy that Jerry can eat. Please note that the sum of deliciousness may exceed the storage range of int
sample input
2
4
1 2 3 1
5
2 7 9 3 1
sample output
4
12

To solve this problem, a dynamic programming method can be used to calculate the maximum sum of deliciousness.

The following is the code implemented in C language:

#include <stdio.h>

int max(int a, int b) {
    
    
    return (a > b) ? a : b;
}

long long int maxCheese(int n, int cheese[]) {
    
    
    long long int dp[100001] = {
    
    0};

    dp[0] = cheese[0];
    dp[1] = max(cheese[0], cheese[1]);

    for (int i = 2; i < n; i++) {
    
    
        dp[i] = max(dp[i - 1], dp[i - 2] + cheese[i]);
    }

    return dp[n - 1];
}

int main() {
    
    
    int T;
    scanf("%d", &T);

    while (T--) {
    
    
        int n;
        scanf("%d", &n);

        int cheese[100000];
        for (int i = 0; i < n; i++) {
    
    
            scanf("%d", &cheese[i]);
        }

        long long int maxSum = maxCheese(n, cheese);
        printf("%lld\n", maxSum);
    }

    return 0;
}

The code uses dynamic programming to calculate the maximum sum of deliciousness. Use the dp array to save the maximum deliciousness sum when reaching each cheese position.

Initially, dp[0] is equal to the deliciousness of the first piece of cheese, and dp[1] is equal to the greater deliciousness of the first and second piece of cheese.

Then, iteratively calculate the dp array starting from the third piece of cheese. For each position i, you can choose not to steal the i-th piece of cheese (keep dp[i-1] unchanged) or steal the i-th piece of cheese (dp[i] = dp[i-2] + cheese[i]) Larger sum of deliciousness.

Finally, dp[n-1] is returned as the result, which is the sum of the maximum delicacy that Jerry can eat.

Hope the above code can help you solve your problem!

Guess you like

Origin blog.csdn.net/gozhuyinglong/article/details/132381968