
Question 1
Problem: Run the following program, the output result is (C).
Analysis: The first condition of the OR operation is true, and the first condition of the AND operation is false, so there is no need to calculate the next operation!
Code:
int func(int y)
{
y -=5;
cout << “X”;
return 0;
}int main() {
int x = 10, y = 5;
if (x > y || func(y))
cout << y;
return 0;
}
A.x0
B.x5
C.5
D.0
Question 2
Problem: Run the following program, the output result is (C).
Code:
int i = 1, t = 0;
while (i * i < 30) {
t += i;
i += 2;
}
cout << t;
A.3
B.4
C.9
D.16
Question 3
Problem: The result of running the statement cout << (char)(‘F’ + 4); is (D).
A.I
B.j
C.K
D.J
Question 4
Problem: Among the following options, the highest priority is (C).
Options:
A.+
B.–
C.*
D.=
Question 5
Problem: Regarding the keywords new and delete, the following statement is incorrect (B).
Options:
1.Memory allocated using new int[10] must be released using delete[].
2.int *p = new int allocated integer memory will be initialized to 0
3.Using delete on a null pointer (nullptr) is safe and will not cause an error.
4.new will throw a std::bad_alloc exception when memory allocation fails, rather than returning a null pointer.
Problem Name: Celebration Queue
Problem Description: There are n volunteers participating in a celebration 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 given number of rows);
2、 The number of volunteers in each row must be the same;
3、 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.
Example: When input n=50, A=11, the output is 4.Explanation:
1、 If 4 people are arranged in each row, 11 rows require 4 * 11 = 44 people (not exceeding 50 people);
2、 If 5 people are arranged in each row, 11 rows require 5* 11 = 55 people (exceeds 50 people, does not meet the requirement). Therefore, at most 4 people 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
Problem Name: Tea Set Combination
Problem Description: Jia Jia 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 tea bowl
1 tea strainer
4 tea cups
Given the stock quantities of teapots, tea bowls, tea strainers, and tea cups, write a program to calculate the maximum number of complete sets that can be formed.
Example: Input: The quantities of teapots, tea bowls, tea strainers, and tea cups are 3,4,2,13. Output: 2. Explanation:
Each set requires 4 tea cups, so the maximum supported by tea cups is13÷4=3 sets;
However, there are only 2 tea strainers, limiting it to 2 sets.
Ultimately, the output is based on the minimum limit, which is 2 sets.
Input Requirements: Input four integers (range 0 ≤ integer ≤ < 100), representing:
1. The number of teapots
2. The number of tea bowls
3. The number of tea strainers
4. The number of tea cups
The 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 16
Output:
2
Problem Name: Balancing Character Swaps at Odd and Even Positions
Problem Description: Given a string 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’ at odd positions
equals
The number of ‘A’ at even positions.
If it is impossible to satisfy the condition through swaps, return -1.
Example: Input: s = “AABABA”
The number of ‘A’ at odd positions (1,3,5) is: 1 (position 1);
The number of ‘A’ at even positions (2,4,6) is: 3 (positions 2,4,6). Operation: Swap the characters at positions 2 and 3 (‘A’ and ‘B’), resulting in “ABAABA”:
The number of ‘A’ at odd positions: 2 (positions 1, 5);
The number of ‘A’ at 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:AABABA
Sample Output:1
Analysis: If there is an odd number of A, then it is impossible to solve. If there is an even number of A, the answer is abs(number of A at even positions – number of A at odd positions) / 2.
Problem Name: Matrix Layer Interleaved Rotation
Problem Description: Given a 2D integer matrix of size n×n , 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 and moving inward, the outermost layer is the first layer, followed by the second layer, the third layer, and so on.
If n is odd, the single element in the center is the innermost layer and does not rotate (value remains unchanged).
Rotation Direction:
The first layer: clockwise rotation 90 degrees.
The second layer: counterclockwise rotation 90 degrees.
The third layer: clockwise rotation 90 degrees.
And so on, the direction alternates.
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 6 7 2
15 10 11 3
16 12 8 4
Explanation::
The first layer (outermost) rotates clockwise 90 degrees.
The second layer (inner layer) rotates counterclockwise 90 degrees.
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 rows, each row inputs n integers ( -1000 element value 1000 ), representing the matrix elements, integers separated by spaces.
Output Requirements: Output n rows, each row n integers, representing the rotated matrix, integers separated by spaces.
Key Points:
Layer division: Process layer by layer from outside to inside.
Alternating rotation direction: Odd layers clockwise, even layers counterclockwise.
Center element: If n is odd, the center element does not rotate.
Data Range
Matrix size 2 2 to 100 100
Element 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 6 7 2
15 10 11 3
16 12 8 4
Analysis: Define two arrays b and c, b is the clockwise array of a, c is the counterclockwise array of a. Then, when outputting, calculate the distance of each position from the boundary. If the distance from the boundary is odd, output the corresponding value from b, otherwise output the corresponding value from c.
Problem Name: Circular Coin Game
Problem Description: Jerry and Tom are playing a game in a circular arrangement of n boxes. Each box i contains a_i coins. The game rules are as follows:
1、 Taking turns to take coins: Jerry goes first, starting from box 1, taking at least 1 coin.
2、 Coin Taking Restrictions:
If the previous player took coins from box i, the current player must take coins from box i+1 .
If the previous player took coins from box n, the current player must take coins from box 1 .
3、 Failure Condition: If it is a player’s turn and the box they must take from has no coins, that player loses.
Assume both players play optimally, determine who will win.
Input Format:
The first line inputs an integer T ( 1≤T≤ 100 ), representing the number of test cases.
Each test case contains two lines:
The first line: n ( 1≤ n ≤ 1000 ), representing the number of boxes.
The second line: n integers a_i ( 1≤ a_i ≤ 10^9 ), representing the number of coins in each box.
Output Format: Output T lines, each line is “Jerry” or “Tom” , indicating the winner of each test case.
Sample Input:1:
2
1
10
2
30 50
Sample Output:
Jerry
Tom
Example Explanation:
In the first test case ( n=1 , a_1=10 ):Jerry takes all 10 coins, and Tom cannot take any, Jerry wins.
In the second test case ( n=2 , a_1=30 , a_2=50 ):Jerry and Tom take turns taking coins, and Tom wins (specific strategy needs further analysis).
Analysis: If there is an even number of boxes, Jerry takes coins from odd positions, and Tom takes coins from even positions. Each time Jerry starts from the first box, and Tom starts from the second box, so whoever runs out of coins first loses. If there is an odd number of boxes, the first player wins, Jerry can take all coins from the first box, and then Tom has no coins to take next.
Problem Name: Magic Playing Card Arrangement
Problem Description: The magician David mixes n red playing cards and n blue playing cards and stacks them on the table. Guests can take any number of cards from the top (at least 1 card, at most 2n cards), requiring that the number of red cards taken is not less than the number of blue cards taken. Calculate the total number of arrangements that satisfy this condition.
Example: Input: n=3
Output: 5 Explanation: For 3 red cards and 3 blue cards, there are the following 5 arrangements that satisfy the condition (using R to represent red cards, B to represent blue cards):
i R R B R B B
ii R B R B R B
iii R B R R B B
iv R R R B B B
v R R B B R B
Input Requirements: Input an integer n ( 1≤ n ≤ 100 ), representing the number of red and blue playing cards (each n cards).
Output Requirements: Output an integer representing the total number of arrangements that satisfy the condition.