The 2025 Blue Bridge Cup for the youth group, both the provincial and national competitions have concluded! Here are the real questions for everyone to study, remember to follow along!
1. Multiple Choice Questions
-
Running the following program, the output result is ( )
int func(int y) {
y -= 5;
cout << “x”;
return 0;
}
intmain() {
int x = 10, y = 5;
if (x > y || func(y))
cout << y;
return 0;
}
A. x0
B. x5
C. 5
D. 0
>> Answer: C
This question tests the operation of logical operators. Since in the if condition, 10>5 is true, func(y) will not execute, and it directly outputs the value of y, which is 5, hence the answer is C.
-
Running the following program, the output result is ( )
int i = 1, t = 0;
while (i * i < 30) {
t += 1;
i += 2;
}
cout << t;
A 3
B 4
C 9
D 16
>> Answer: A
This program counts the number of odd numbers whose squares are less than 30. According to the question, i can take values 1, 3, 5, totaling 3, hence the answer is A.
-
Running the statement cout << (char)(‘F’ + 4); the result is ( )
Options:
A I
B j
C K
D J
>> Answer: D
This question involves type casting and ASCII codes. In fact, this question outputs the 4th letter after the uppercase letter F, which is J, hence the answer is D.
-
Among the following choices, the highest precedence is ( )
A +
B –
C *
D =
>> Answer: C
This question tests knowledge of operator precedence. Among the four options, = has the lowest precedence, while * has the highest, hence the answer is C.
-
Regardingthe new and delete keywords in C++, the following statement is incorrect ( )
A. The memory allocated by new int[10] must be released using delete[ ]
B. The memory allocated by int *p = new int; will be initialized to 0 by default
C. Using delete on a null pointer (nullptr) is safe and will not cause an error
D. When new fails to allocate memory, it throws std::bad_alloc exception by default, rather than returning a null pointer
>> Answer: B
This question tests knowledge of dynamic memory allocation. The new keyword is used for allocation, and delete is used for deallocation, or alternatively, the C language’s malloc() function can be used for allocation and free() for deallocation. When dynamically allocating memory, if successful, it returns the memory address. If it fails, there are two scenarios: one is when using malloc(), it returns a null pointer to avoid errors; the other is when using new, it throws std::bad_alloc exception by default, rather than returning a null pointer. This is the default behavior of C++, aimed at handling memory allocation failures promptly through exceptions, avoiding undefined behavior due to unhandled null pointers. A null pointer is a pointer that does not point to any valid memory address or a specific object, and deleting a null pointer will not cause an error. If you want new to return a null pointer when memory allocation fails, you can use the new (std::nothrow) form, for example:
int* ptr = new (std::nothrow) int[100]; if (ptr == nullptr) { // handle memory allocation failure }
In this case, the new operator will return a null pointer on failure, rather than throwing an exception, requiring manual checking of the pointer for null and corresponding error handling. After successful memory allocation, no default initialization will occur. If initialization is needed, it can be done using methods such as “char*p=new char(‘A’)” or “int *p=new int[10]” during the allocation process. Hence the answer is B.
2. Programming Implementation
-
Ceremony Queue
Problem Description
There are n volunteers participating in a ceremony event, and they need to be arranged in a rectangular queue. The queue must meet the following conditions:
1. There are A rows (A is the number of rows given as input);
-
Each row must have the same number of volunteers;
-
Not all volunteers need to be arranged in the queue (some can remain unarranged).
The goal is to calculate the maximum number of volunteers that can be arranged in each row.
Input Requirements
Input two integers n and A (separated by a space), satisfying:
2 ≤ n ≤ 500 (total number of volunteers);
2 ≤ A ≤ n (number of rows in the queue).
Output Requirements
Output an integer representing the maximum number of volunteers that can be arranged in each row.
Sample Input
50 11
Sample Output
4
>> Answer:

2. Tea Set Combination
Problem Description
Jiajia works in a tea set store and needs to calculate the maximum number of complete tea sets that can be formed based on customer orders. A complete tea set includes the following components:
1 teapot, 1 lid bowl,
1 tea strainer,
4 teacups.
Given the stock quantities of teapots, lid bowls, tea strainers, and teacups, write a program to calculate the maximum number of complete tea sets that can be formed.
Input Requirements
Input 4 integers (range 0 ≤ integer ≤ 100), representing:
-
The number of teapots
-
The number of lid bowls
-
The number of tea strainers
-
The number of teacups
Integers are separated by spaces.
Output Requirements
Output an integer representing the maximum number of complete tea sets that can be formed.
Sample Input and Output
Input:
3 4 2 13
Output:
2
>> Answer:
Method 1 (Mathematical):Check how many complete tea sets can be formed from each component, the minimum value among them is the answer. It should be noted that the min({a,b,c,d/4}) function can be used after C++11.

Method 2 (Simulation):Combine one set at a time until a complete tea set can no longer be formed.


3. Balancing Odd and Even Positions with Character Swaps
Problem Description
Given a string S consisting only of characters ‘A’ and ‘B’. Each operation can swap two adjacent characters. The goal is to achieve the following condition with the minimum number of swaps:
The number of ‘A’s in odd positions (position numbering starts from 1) must equal
the number of ‘A’s in even positions.
If it is impossible to satisfy the condition through swaps, return -1.
Example
Input: S = “AABABA”
Number of ‘A’s in odd positions (1, 3, 5): 1 (position 1)
Number of ‘A’s in even positions (2, 4, 6): 3 (positions 2, 4, 6)
Operation: Swap characters at positions 2 and 3 (‘A’ and ‘B’), resulting in “ABAABA”
Number of ‘A’s in odd positions: 2 (positions 1, 5)
Number of ‘A’s in even positions: 2 (positions 2, 6)
Output: 1 (minimum number of swaps)
Input Requirements
Input a string S (2 ≤ |S| ≤ 10^5), consisting only of ‘A’ and ‘B’.
Output Requirements
Output the minimum number of swaps; if it is impossible to satisfy the condition, output -1.
Sample Input and Output
Input:
AABABA
Output:
1
>> Answer:

4. Matrix Layer Interleaved Rotation
Problem Description
Given a n x n two-dimensional integer matrix, each “layer” of the matrix needs to be interleaved rotated (clockwise and counterclockwise alternately). The specific rules are as follows:
1. Layer Definition:
Starting from the outermost layer inward, the outermost layer is the 1st layer, followed by the 2nd layer, the 3rd layer, and so on.
If n is odd, the single center element is the innermost layer and does not rotate (its value remains unchanged).
2. Rotation Direction:
The 1st layer: Rotate 90 degrees clockwise.
The 2nd layer: Rotate 90 degrees counterclockwise.
The 3rd layer: Rotate 90 degrees clockwise.
And so on, alternating the direction.
3. Rotation Range:
Each layer’s rotation only affects the elements within that layer.
Example
Input( n=4 ):
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
13 9 5 1
14 7 11 2
15 6 10 3
16 12 8 4
Explanation:
The 1st layer (outermost) rotates 90 degrees clockwise.
The 2nd layer (inner layer) rotates 90 degrees counterclockwise.
Input Requirements
-
The first line inputs a positive integer n (2 ≤ n ≤ 100), representing the number of rows and columns of the matrix.
-
The next n lines, each input n integers (-1000 ≤ integer ≤ 1000), representing the matrix elements, separated by spaces.
Output Requirements
Output n lines, each containing n integers, representing the rotated matrix, with integers separated by spaces.
Key Points
1. Layer Division:Process layer by layer from outside to inside.
2. Alternating Rotation Direction:Odd layers rotate clockwise, even layers rotate counterclockwise.
3. Center Element:If n is odd, the center element does not rotate.
Data Range
Matrix size: 2×2 to 100×100.
Element value range: -1000 to 1000.
Sample Input and Output
Input:
4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
13 9 5 1
14 7 11 2
15 6 10 3
16 12 8 4
>> Answer:This question focuses on mapping matrix elements. Since the matrix size is uncertain, it is recommended to use a two-dimensional dynamic array for operations. Key points include:First, accurately find the index conversion relationship of elements;Second, effectively handle the switching between clockwise and counterclockwise rotations. Since there are two states, consider setting a boolean variable;Third, carefully determine the relationship between the number of layers, edge lengths, starting points, and ending elements, etc.


