CSDN Programming Contest Thirty-two Questions

Competition overview

CSDN programming competition thirty-two: competition details (csdn.net)

competition solution

Topic 1. Legendary Hegemony

Legendary hegemony, if you are a brother, you can do it. Xiaochun (HP is a) meets a golden goblin (HP is x). Xiaochun can cause b points of damage to the goblin each time, and the goblin can cause y points of damage to Xiaochun each time. As a player, how could Xiaochun casually let goblins kill him! He has healing magic medicine, which can restore c points of HP every time. HP unlimited. How many operations (healing + attack) does Xiaochun need to kill the goblin?

int main () {
    int a, b, c, x, y;
    scanf ("%d %d %d %d %d", &a, &b, &c, &x, &y);
    role player, monster;
    player.hp = a; player.attack = b;
    monster.hp = x; monster.attack = y;
    int attack_num = (monster.hp / player.attack) + (monster.hp % player.attack != 0 ? 1 : 0);
    int attack_monster = monster.attack * (attack_num - 1);
    int t = 0;
    while (player.hp + t * c <= attack_monster){
        t = t + 1;
        attack_monster = attack_monster + monster.attack;
    }
    return 0;
}

Although the description of this question feels like a real-time game, it actually uses turn-based rules, similar to Purcell playing Pony.

In this question, the rules have been simplified a lot. There are no restrictions on the number of skill releases (PP) and skill cooling time (CD). There is no need to move to avoid the monster's attack, and the monster will not recover blood.

The required number of attacks can be directly calculated. After the player attacks, it is the monster's turn to attack, and the damage done by the monster to the player can be calculated from this.

Generally speaking, the player's HP is not enough to resist the total damage of the monster, and there is a high probability that recovery items will be needed during the attack. However, friends who have played Purcell should know that after using props to recover, they will be beaten by monsters once. Therefore, the process of taking medicine needs to be calculated separately. After taking medicine each time, update the number of times the player takes medicine and the number of monster attacks until the sum of the initial HP and the recovered HP exceeds the total output of the monster.

The final result is the sum of the number of attacks and doses.

Another greedy way of thinking can also be used to solve this problem. When the boss can be killed directly or the blood volume is enough to resist the boss's attack, the player adopts an attack strategy; otherwise, the player must take medicine. This kind of thinking can be done directly without brain circulation, and the program can help calculate the answer without too much analysis.

Topic 2. Strictly investigate gunfire

Country X has recently begun to strictly control gun fire. Like ak, skr, m4a1. All are forbidden. Now Xiao Q has seized a batch of prohibited items, some of which are guns. Xiao Q wants to know how many people he needs to imprison according to his possession of guns (only the above three guns are considered illegal).

int main () {
    int result = 0;
    std::string str, data [] = {"ak", "skr", "m4a1"};
    while (std::cin >> str) {
        for (int i = 0; i < 3; i++) {
            if (str == data [i]) result += 1;
        }
    }
    return 0;
}

This question can directly compare strings after input, and it is more efficient to do so. However, in order to reflect the idea of ​​program automation, it is recommended to store all qualified character strings in a list and use loops to automatically compare them.

When the comparison of the input data is successful, the result is updated until the comparison of all the input data is completed, and the final result is output. 

Topic 3. Ant family

The small ant colony is a huge group. There are n small ants in this ant colony. In order to ensure that all ants can receive the message when the message is transmitted, it is necessary to establish a communication relationship between them. It is required that all ants can contact other people through multiple ants or directly. It is known that there are communication relationships between several small ants, how many relationships do we need to create at least?

This question has been tested once before, and it can be solved with the union search algorithm.

Topic 4. Transporting oil

An oil company needs to transport oil to places A and B. The demand in the two places is different, and a car can only carry a certain amount of oil. After calculation, A needs a vehicle for transportation, and B needs b vehicles for transportation, so as to meet the demand. Now there are a total of n vehicles distributed in various places, and each vehicle can obtain a certain amount of profit for transporting oil between A and B. Now please arrange a car to go to A, and b to transport oil to B, so that the maximum profit can be obtained under the premise of meeting the oil demand of A and B. Each vehicle can only transport oil to one place.

#include <cstdio>

int dp [1005][1005];

int main () {
    int n, a, b;
    scanf ("%d %d %d", &n, &a, &b);
    for (int i = 1; i <= n; i++) {
        scanf ("%d %d", &award [0], &award [1]);
        for (int x = min (a, i), x_end = max (a - n + i, 0); x >= x_end; x--) {
            for (int y = min (b, i - x), y_end = max (a + b - n + i - x, 0); y >= y_end; y--) {
                if (x + y > i) {
                    break;
                } else if (x == 0 && y == 0) {
                    continue;
                } else if (x == 0) {
                    dp [x][y] = max (dp [x][y], dp [x][y - 1] + award [1]);
                } else if (y == 0) {
                    dp [x][y] = max (dp [x][y], dp [x - 1][y] + award [0]);
                } else {
                    dp [x][y] = max (dp [x][y], dp [x - 1][y] + award [0], dp [x][y - 1] + award [1]);
                }
            }
        }
    }
    printf ("%d", dp [a][b]);
    return 0;
}

Guess you like

Origin blog.csdn.net/x1051496412/article/details/129265498