CSDN Programming Contest Thirty-three Questions

Competition overview

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

competition solution

Topic 1. Odd-even sorting

Given an array of integers, rearrange the array so that the left side of the array is odd and the right side is even (the order of odd and even numbers is arranged according to the order of the input numbers).

The original question of the seventh competition.

There are many solutions.

Solution 1: When encountering an odd number, output it directly; when encountering an even number, save it, and finally output it uniformly.

Solution 2: Store odd numbers and even numbers in two lists, and finally output them together.

Solution 3: Use a stable sorting algorithm (stable does not mean that the results of the algorithm are fixed, but that the relative order of the elements is not changed when sorting, for example, two characters with the same level 1 have different ids, and the one with the larger id is in front. If you use stable If the sorting algorithm is used, when sorting by level, the ids of the two roles of the same level will still maintain the original order after sorting, instead of the one with the smaller id at the front). You can magically change the bubble sort to complete this question.

Solution 4: Let’s dig it yourself, there are really many methods, and you can come up with a lot of them with active thinking.

Although there are many solutions to this question, the blogger chose a simpler writing method so that he can pass this question in a shorter time.

Topic 2. Xiaoyi adapts strings

Given the string str, add at least how many characters can make str become a palindrome.

The original question of the eighth competition.

Do you feel that the idea of ​​this question is somewhat similar to a classic string dynamic programming problem?

That's right, in the classic LCS (longest common subsequence) problem, the state transition equation is also like this.

In this question, the original string is regarded as string 1, and the reversed result is regarded as string 2, and the LCS result is calculated.

The final answer is the length of the string minus the LCS result.

As for why this is possible, I leave it to the reader to think for himself. The reason is not complicated. When you suddenly want to understand, there will be a feeling of enlightenment.

Topic 3. The company's new table

In order to highlight the characteristics of the company. An n-ary table is installed. The time of the new table is known to be H:M. The legal definition of time is H<=23 && M<=59. How many ways to define the time, and print them out in sequence. If there are an infinite number of solutions output -1, there is no output 0.

The original title of the thirteenth competition.

bool match (std::string str, int base) {
    int time [2] = {0, 0}, t = 0;
    for (int i = 0; i < str.length (); i++) {
        if (str [i] == ':') {
            t = 1;
            continue;
        }
        int x = (str [i] >= '0' && str [i] <= '9') ? (str [i] - '0') : (str [i] - 'A' + 10);
        if (x < base) time [t] = time [t] * base + x; else return false;
    }
    return time [0] < 24 && time [1] < 60;
}

The smallest base is binary. For time, the maximum is base 60. Check 2 to 60 in turn, and record the values ​​that meet the conditions. After all judgments are completed, if the list is empty, output 0; if base 60 is available, output -1 (indicating that there are countless solutions); otherwise, output the test results in order.

Topic 4. Choose an inn

There are n distinctive inns by the Lijiang River, and the inns are numbered from 1 to n according to their location. Each inn is decorated according to a certain color (a total of k types, represented by integers 0 to k-1). And each inn has a coffee shop, and each coffee shop has its own minimum consumption. Two tourists travel to Lijiang together. They like the same color tone and want to try two different inns, so they decide to live in two inns with the same color tone. In the evening, they plan to choose a coffee shop to drink coffee, requiring the coffee shop to be located between the two inns where they live (including the inn they live in), and the minimum consumption of the coffee shop should not exceed p yuan. They want to know how many accommodation options there are in total, and they can guarantee that they can find a coffee shop with a minimum consumption of no more than p yuan for a small gathering at night.

The original title of the fifteenth competition.

The original question of NOIP, the code is too lazy to post, and you can find a lot of codes by searching it yourself.

Guess you like

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