CSDN Programming Contest Thirty-seven Questions

Competition overview

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

There have been some new problems in recent competitions, and no new bugs have been encountered throughout the competition.

In addition, the processing speed of complaints about plagiarism and problem solving is also very timely, allowing bloggers to see the progress of Station C.

Although the problem of individual users opening a small account still occurs frequently, the overall experience is getting better and better.

competition solution

Topic 1. Kindergarten homework

The Kindergarten class finally has new homework. The teacher arranged for the students to use the 4 wooden sticks distributed to the students to splice them into a triangle. Of course, according to the normal logic, if it cannot be spliced ​​into a triangle, a certain wooden stick must be broken to splice the triangle. But of course the lazy Xiaoyi won't work hard! If the splicing fails to form a triangle, Xiaoyi will splice it into a pseudo triangle similar to a side length of 1 1 2 (the sum of the two sides is equal to the third side). If the pseudo-triangles cannot be spliced ​​together, then the homework will not be handed in!

The final answer is just a number, and there are very few types of answers. I didn't bother to think, so I did it directly by defrauding points.

Topic 2, XOR and

Xiao Zhang found an integer N, and he wanted to ask you what is the XOR sum of all the different integers from 1 to N? Please answer his question. This question is provided by CSDN user a23333a .

#include <cstdio>

int main () {
    int resu1t = 0;
    int n;
    scanf ("%d", &n);
    while (n > 0) resu1t = resu1t ^ n --;
    return 0;
}

The test data is relatively friendly, and direct violence simulation is enough.

Topic 3. Large integers replace digits

You are given an integer N of length M in the form of a string. Please calculate all possible different operations that have a modulo 9 value of 1 after performing an operation on this number. In one operation, we can select a digit A of N and replace it with a different digit B in the range 0 to 9. The two modes of operation are different if and only if the selected positions or the number of replacements are different. This question is provided by CSDN user a23333a .

I believe everyone has learned how to judge multiples of 3. Sum each digit of the number, and then divide by 3 to see if the remainder is zero, you can know whether the number is a multiple of 3. If the remainder is 1, then satisfy SUM % 3 == 1.

The same is true for multiples of nine. You only need to sum each digit to determine whether the remainder of 9 is zero. For example, 27, 108, etc., are all multiples of 9.

This question only needs to judge the number after replacement, if SUM % 9 == 1, it can be replaced. In addition, if the number to be replaced is the same as the number on the original number, it cannot be replaced and should be skipped directly.

From the above reasoning, we can see that when replacing, the sum of digits either becomes smaller or larger. Therefore, there is still an optimization point for this question. You can know whether this bit can be replaced without trying them one by one. Interested friends can think about it for themselves.

Topic 4. Inexplicable keyboard

There is a magic keyboard with which you can enter characters from a to z, however whenever you enter one of the vowel letters (a, e, i, o, u), the entered string will A reversal! For example, if tw is currently input, and an o is input at this time, the string two on the screen will be reversed into owt. Given a character string, how many ways can it be obtained by using this keyboard to input it?

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>

const std::string spec = "aeiou";

int search (std::string str) {
    if (str.length () == 1) return 1;
    int result = 0;
    if (match (str [0]) == true) {
        std::string inv = str.substr (1);
        std::reverse (inv.begin (), inv.end ());
        result = result + search (inv);
    }
    if (match (str [str.length () - 1]) == false) {
        result = result + search (str.substr (0, str.length () - 1));
    }
    return result;
}

 This question can be reasoned with ideas similar to dynamic programming.

When the length of the string is 1, it can be obtained by directly inputting it. There are 1 ways.

When its length exceeds 1, it can be transferred from a shorter string state.

For example, when the target string is ac:

1. It can be obtained by adding an a (namely ca) to c and forcing it to flip.

2. It can be obtained by adding 1 c to a and directly inputting it. c is not one of aeiou, so the result will not be flipped.

Through the state transfer in these two ways, the short string can be expanded into a long string to obtain the number of schemes.

Guess you like

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