1. Multiple Choice Questions
Question 1
Problem: Run the following program, the output result is () .
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. XO
B. X5
C. 5
D. 0
Question 2
Problem: Run the following program, the output result is () .
Code:
int i=1,t=0;while(i *i<30){t += 1;i += 2;}cout << t;
A. 3
B. 4
C. 9
D. 16
Question 3
Problem: The result of the statement cout<<(char)(‘F’+ 4); is ( )
A. I
B. j
C. K
D. J
Question 4
Problem: Among the following choices, the highest priority is () .
A. +
B. –
C. *
D. =
Question 5
Problem: Regarding the new and delete keywords in C++, the following statement is incorrect () .
1. The memory allocated by new int[10] must be released using delete[] – correct
2. int *p = new int; The allocated integer memory is initialized to 0 by default – incorrect (not initialized by default)
3. Using delete on a null pointer (nullptr) is safe and will not cause an error – correct
4. new throws std::bad_alloc exception by default when memory allocation fails, rather than returning a null pointer – correct
2. Programming Questions
A Celebration Queue
Problem Description
There are <span><span>n</span></span> volunteers participating in the celebration event, and they need to be arranged in a rectangular queue that meets the following conditions:
- There are a total of
<span><span>A</span></span>rows (<span><span>A</span></span>is the given number of rows); - The number of volunteers in each row must be the same;
- Not all volunteers need to be arranged in the queue (some can be left out).
The goal is to calculate the maximum number of volunteers that can be arranged in each row.
Example
- Input:
<span><span>n = 50</span></span>,<span><span>A = 11</span></span> - Output:
<span><span>4</span></span> - Explanation:
- If 4 people are arranged in each row, 11 rows require (4 * 11 = 44) people (not exceeding 50);
- If 5 people are arranged in each row, 11 rows require (5 * 11 = 55) people (exceeding 50, not meeting the condition);
- Therefore, at most 4 people can be arranged in each row.
Input Requirements
Input two integers <span><span>n</span></span> and <span><span>A</span></span> (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 and Output
Input:<span><span>50 11</span></span>Output:<span><span>4</span></span>B Tea Set CombinationProblem Description
Given the inventory quantities of the above four components, write a program to calculate the maximum number of complete sets that can be formed.
Example
- Input: The quantities of teapots, gaiwans, tea strainers, and teacups are 3, 4, 2, 13 respectively
- Output: 2
- Explanation:
- Each 4 teacups can support 1 set, 13 teacups can support at most 3 sets (13 ÷ 4 = 3, taking the integer part).
- However, there are only 2 strainers, which can support at most 2 sets.
- Ultimately limited by the component with the least quantity (the strainer), output 2 sets.
Input Requirements
Input 4 integers (range 0 ≤ integer ≤ 100), representing:
- The number of teapots
- The number of gaiwans
- 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 set combinations that can be formed.
Sample Input and Output
- Input: 3 4 2 13
- Output: 2
C Balance Odd-Even Position Character Swap
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) equals the number of ‘A’s in even positions.
If it is impossible to satisfy the condition through swapping, return -1.
Example
- Input: S = “AABABA”
- Original State Analysis:
- Number of ‘A’s in odd positions (1, 3, 5): 1 (only position 1 is ‘A’)
- Number of ‘A’s in even positions (2, 4, 6): 3 (positions 2, 4, 6 are all ‘A’)
- Operation: Swap characters at positions 2 and 3 (‘A’ and ‘B’), resulting in “ABAABA”
- State After Operation:
- 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), containing only ‘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:AABABAOutput:1D Matrix Layer Interleaved Rotation
Problem Description
Given an 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:
-
Layer Definition:
- Starting from the outermost layer and moving 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 element in the center is the innermost layer and does not rotate (value remains unchanged).
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.
Rotation Range:
- The 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 114 6 7 215 10 11 316 12 8 4Explanation
- 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.
- Next n lines, each input n integers (-1000 ≤ integer ≤ 1000), representing the matrix elements, integers separated by spaces.
Output Requirements
Output n lines, each line 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.
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
E Circular Coin Game
Problem Description
Jerry and Tom are playing a game in a circular arrangement of n boxes, each containing a_i coins. The game rules are as follows:
- Taking Coins AlternatelyJerry goes first, starting from box 1 and must take at least 1 coin.
- 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.
Assuming both players adopt optimal strategies, determine who will win.
Example
Input:
2
1
10
2
30 50
Output:JerryTom
Example Explanation:
- Jerry and Tom take coins alternately, and Tom wins in the end (specific strategy needs further analysis).
- Jerry takes all 10 coins, and Tom cannot take any, Jerry wins.
- First group of data (n=1,a1=10):
- Second group of data (n=2,a1=30,a2=50):
Input Format
- The first line inputs an integer T (1≤T≤100), indicating the number of test cases.
- Each test case contains two lines:
- The first line: integer n (1≤n≤1000), indicating the number of boxes.
- The second line: n integers a_i (1≤a_i≤10^9), indicating the number of coins in each box.
Output Format
Output T lines, each line is “Jerry” or “Tom”, indicating the winner for each test case.
F Magic PokerCard Arrangement
Problem Description
Magician David mixes <span><span>n</span></span> red playing cards and <span><span>n</span></span> blue playing cards and stacks them on the table. Guests can take any number of cards (at least 1, up to 2n) from the top, requiring that no matter how many cards the guests take, the number of red cards must be at least the number of blue cards. Calculate the total number of arrangements that satisfy this condition.
Example
-
Input:
<span><span>n = 3</span></span> -
Output:
<span><span>5</span></span> -
Explanation: For 3 red and 3 blue cards, there are the following 5 arrangements that satisfy the condition (using R for red cards and B for 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 <span><span>n</span></span> (1 ≤ n ≤ 100), representing the number of red and blue playing cards (each <span><span>n</span></span><span><span> cards).</span></span>
Output Requirements
Output an integer representing the total number of arrangements that satisfy the condition.
Sample Input and Output
Input:3Output:5