CSDN programming competition forty-three problem solutions

Competition overview

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

competition solution

Topic 1. Judging the winner

Two strings A and B are known. Read in n times continuously, and the character string read in each time is A or B. Output the string that was read the most times.

This question itself is not difficult, and when you encounter a string, give its count +1.

Since there are only two strings, simply record the first occurrence of the string. When a new string is encountered, if it is consistent with the first string, update counter A, otherwise, update counter B,

However, what if there are multiple strings? What should we do at this time?

The blogger gives an idea here, and interested friends can try it out by themselves.

First, use an array to record the strings that have appeared, and open up another array as a counter to count the number of occurrences of the corresponding string. You can also directly use a structure to store the content of the string and the number of times it appears, so you only need to open up an array.

When a new string is encountered, check the array for a consistent one. If so, update the corresponding counter. Otherwise, store the newly encountered string at the end of the array, and set the value of the counter to 1, indicating that the string appears once.

This question should be regarded as the easiest question in this competition, and most people, including beginners, should be able to pass it.

Topic 2. Moving the little guinea pig

The little guinea pigs sit in a row. Xiao Yijiang bought a small house n*m ​​in rows of grids, and she wanted k little guinea pigs to have their own house. But in order not to waste space, she wants the outermost circle of the small house to have a small guinea pig living in each row and column as much as possible (the guinea pig can also live in the middle grid, just make sure that the outermost row and column of the house live at least one only guinea pigs, not every row and column). Xiao Yijiang wanted to know how many plans she had for arranging the little guinea pig.

Using the deep search method, try to arrange the guinea pigs into various positions.

When all the guinea pigs are arranged, a set of plans can be generated, and the plans will be verified at this time. If the requirements of the topic are met, it means that this scheme is reasonable, and the counter is updated immediately at this time.

After the search is completed, the total number of feasible solutions can be obtained.

Topic 3. The Drunken Jailor

A certain prison has a hall consisting of n cells, each of which is next to each other. There is one prisoner in each cell, and each cell is locked. One night, the jailer got bored and decided to play a game. In the first round, he drank a glass of whiskey and ran down the hall, unlocking every cell. In the second round, he drank a glass of whiskey and ran down the hall to lock every other cell (cells 2, 4, 6....). In the third round, he drank a whiskey and ran down the hall. He went to every third cell (cells 3, 6, and 9). If the cell was locked, he opened it; if the cell door was open, he locked the cell. He repeats for n rounds, drinks the last cup, and passes out. Some prisoners (possibly number zero) realize that their cell is unlocked and the jailer is incapacitated, and they can escape immediately. Now based on the number of cells, determine how many prisoners escaped.

The description of this question is relatively long, but it is not difficult. As long as you read the question patiently and analyze it a little, you can find out the rules.

First of all, all rooms are locked in the initial state, but the jailer will unlock the locks of all rooms in the first round. In other words, we only need to start processing from the second round, when all rooms are unlocked.

Then use the simulation method to judge each room individually to get the number of lock operations. If the number of times is even, the locking and unlocking operations can all be offset, and the prisoner in this room can go out; otherwise, the prisoner is locked and cannot go out.

After scanning all the rooms, the number of escaped prisoners can be counted.

Topic 4. Meeting arrangement

It's a meeting! As a group, of course the table needs to be adjacent to the boss during the meeting (the boss may sit on the table)! Xiaoyi was assigned to arranging tables and chairs, but Xiaoyi spent all her energy on eating, how could she move these tables and chairs. She decided to use the existing layout as a conference seating arrangement. Each table is assigned one person, and adjacent tables are of different colors. Xiaoyi wants to know how many people can surround the boss after choosing a certain table?

This question was done in the 20th competition. Considering that the range of answers is not very large, it was directly cheated.

Coincidentally, a week before this competition, there were some new fans who read the article. The blogger will read the articles that these fans have read, and take a look at the corresponding code by the way. As a result, this question came up again...

The blogger found that although some topics have appeared many times, some optimizations have been made. For example, the description of the drunk jailer has a few more words: it may be zero.

So, here is also a suggestion by the way. If the questions will be repeated in subsequent competitions, the test data can be updated appropriately (for example, use a data generator to generate a certain proportion of new data for each question). In some previous competitions, data errors occasionally occurred. If new data can be generated each time, the probability of this situation can be reduced to a certain extent.

Guess you like

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