11、The following C++ code is used to find the mirror number of N (the digits of N are reversed from the least significant to the most significant, but leading 0 will be ignored and not output), for example, if the input is 1234, the output will be 4321 , and if the input is 120, the output will be 21 , the incorrect option is ( ).
cout << "Please enter a positive integer with the last digit not being 0:";cin >> N;rst = 0; // Save the reversed result while (______________){ rst = rst * 10 + N % 10; N = N / 10;}cout << rst << endl;
A. N != 0
B. not (N == 0)
C. N = 0
D. N > 0
【Analysis】
This question is about finding the mirror number of a number N. The program uses a loop to reverse the number. The loop condition needs to ensure that it continues executing when N is not 0, because in each loop, we take the last digit of N (by N % 10), then add it to rst, while dividing N by 10 (integer division) to remove the last digit.
According to the C++ code logic, the program is used to find the mirror number of a positive integer N. The loop condition needs to ensure that it continues executing while N is not 0, thus processing all digit places. Analyzing each option:
A. N != 0: Correct, when N is not 0, the loop continues, correctly handling the number reversal.
B. not (N == 0): Correct, equivalent to N != 0, functionally the same as A.
C. N = 0: Incorrect, this is an assignment expression that sets N to 0, causing the loop condition to always be false, the loop will not execute, and the number reversal cannot be achieved.
D. N > 0: Correct, since N is a positive integer, and the last digit is not 0, in the loop, N gradually decreases to 0, N>0 ensures that the loop continues while N is positive.
Therefore, the answer to this question is C.
12、 The following C++ code is used to swap the values of two positive integers a and b, the code that cannot achieve the swap is ( ).
A、
cout << "Enter the first positive integer: ";cin >> a;cout <<"Enter the second positive integer: ";cin >> b;temp = a;a = b;b = temp;cout << "a=" << a << " b=" << b << endl;
B、
cout << "Enter the first positive integer: ";cin >> a;cout <<"Enter the second positive integer: ";cin >> b;b = a - b;a = a - b;b = a + b;cout << "a=" << a << " b=" << b << endl;
C、
cout << "Enter the first positive integer: ";cin >> a;cout <<"Enter the second positive integer: ";cin >> b;a = a + b;b = a - b;a = a - b;cout << "a=" << a << " b=" << b << endl;
D、
cout << "Enter the first positive integer: ";cin >> a;cout <<"Enter the second positive integer: ";cin >> b;a, b = b, a;cout << "a=" << a << " b=" << b << endl;
【Analysis】
This question requires us to find which option cannot correctly swap the values of two positive integers a and b. We need to analyze the code of each option.
According to the C++ syntax and code logic analysis, option D cannot achieve the swap of two positive integers a and b. The reason is that C++ does not support the tuple assignment syntax found in Python, which would lead to a compilation error. Other options can correctly swap the variable values:
A: Uses a temporary variable temp, which is the standard swapping method, correct.
B: Uses arithmetic operations to achieve the swap through a series of subtractions and additions, effective for positive integers.
C: Uses arithmetic operations to achieve the swap through addition and subtraction, effective for positive integers.
Therefore, the answer to this question is D.
【Knowledge Expansion】
1、Variable Swapping Methods: In C++, common swapping methods include using temporary variables, arithmetic operations, or bitwise operations. Using the standard library function std::swap is the simplest and safest way.
2、Syntax Differences: C++ differs from languages like Python in assignment syntax, C++ does not support simultaneous assignment of multiple variables and must be done step by step.
3、Limitations of Arithmetic Swapping: Although options B and C can swap positive integers, if large values are involved, it may lead to integer overflow, or unexpected results when dealing with negative numbers. Therefore, in practical programming, it is recommended to use temporary variables or std::swap.
13、 The following C++ code is used to obtain the Mth digit of a positive integer N, where the unit digit is the first digit. For example, if N equals 1234, and M equals 2, the output will be 3. Assuming M is greater than or equal to 1 and less than or equal to the number of digits in N, the code to fill in the blank is ( ).
int N, M, div=1;cout << "Please enter a positive integer:";cin >> N;cout <<"Please enter which digit to take from right to left:";cin >> M;for (int i =0; i < (M - 1); i++) div *= 10;cout << (______________);
A. N % div / 10
B. N / div / 10
C. N % div % 10
D. N / div % 10
【Analysis】
According to the C++ code logic, the program is used to obtain the Mth digit of a positive integer N (where the unit digit is the first digit). In the code, the loop calculates div as 10 raised to the power of (M-1), which is used to move the Mth digit to the unit position. Then, through the expression N / div % 10, we can extract the digit.
D:N / div % 10 correctly implements the above logic. For example, if N=1234, and M=2, then div=10, N/div=123, 123%10=3, output is correct.
Other options are incorrect:
A. N % div / 10: For example, if N=1234, and M=2, then N%div=4, 4/10=0, incorrect.
B. N / div / 10: For example, if N=1234, and M=2, then N/div=123, 123/10=12, incorrect.
C. N % div % 10: For example, if N=1234, and M=2, then N%div=4, 4%10=4, incorrect.
Therefore, the answer to this question is D.
【Knowledge Expansion】
1、Digit Extraction: In C++, extracting specific digits of a number usually combines division (/) and modulus (%) operations. For example:
① Unit digit:N % 10
② Ten’s digit:(N / 10) % 10
③ Hundred’s digit:(N / 100) % 10
2、Loop Constructing Divisor: The loop in the code is used to dynamically calculate the divisor div, avoiding dependence on fixed digit counts, making the code more general.
3、Application Scenarios: This technique is often used in algorithms for number reversal, palindrome verification, digit separation, etc.
14、 The following C++ code outputs ( ).
num = 0;while (num <= 5){ num += 1; if (num == 3) continue; printf("%d#", num);}
A. 1#2#4#5#6#
B. 1#2#4#5#6
C. 1#2#3#4#5#6#
D. 1#2#3#4#5#6
【Analysis】
Based on the execution process of the C++ code:
1、Initialization num = 0
2、Enter the while loop, the condition num <= 5 is true, execute the loop body
3、In each loop, first execute num += 1, then check if (num == 3), if true, execute continue, skipping the remaining code of this loop (i.e., not executing printf).
1、Step by step execution:
① First loop:num becomes 1, not equal to 3, print 1#
② Second loop:num becomes 2, not equal to 3, print 2#
③ Third loop:num becomes 3, equal to 3, execute continue, do not print
④ Fourth loop:num becomes 4, not equal to 3, print 4#
⑤ Fifth loop:num becomes 5, not equal to 3, print 5#
⑥ Sixth loop:num becomes 6, not equal to 3, print 6#, at this point num is 6, the loop condition num <= 5 becomes false, the loop ends.
The output result is 1#2#4#5#6#.
Therefore, the answer to this question is A.
【Knowledge Expansion】
1、Continue Statement: Using continue in a loop will skip the remaining code of the current iteration and directly enter the condition check for the next iteration. In this question, when num is 3 , continue skips the printf statement.
2、Loop Control: In the while loop, the loop condition is checked at the beginning of each iteration. Note the position of the loop variable update, in this question num += 1 is at the beginning of the loop body, thus affecting the number of iterations and output values.
3、Output Format: The printf adds # after each output, thus the output string ends with #, option A conforms to this format.
15、 The following C++ code is used to record the maximum and minimum numbers among multiple input numbers (input -999 ends the input), which statement is incorrect ( ).
cin >> now_num;min_num = max_num = now_num;while (now_num != -999){ if (max_num < now_num) max_num = now_num; if (min_num > now_num) min_num = now_num; cin >> now_num;}cout << min_num << ' ' << max_num;
A. If the first number input is -999, the output will be -999 -999
B. If the first number input is not -999, then if there is no -999 in the data to be input, the program can find the maximum and minimum numbers among the input integers
C. If used for inputting exam scores, that is, scores cannot be -999, then the program can find the highest and lowest scores among the input scores
D. It is possible to move cin >> now_num; to below while (now_num != -999) { and the result will remain unchanged
【Analysis】
According to the logic of the C++ code, the program is used to input multiple numbers, using -999 as an end marker to record the minimum and maximum values among them. The following is an analysis of each option:
A: Correct. If the first number input is -999, then min_num and max_num will both be initialized to -999, the loop will not execute, and the output will be -999 -999.
B: Correct. If the first number is not -999 and there is no -999 in the input, the program will enter an infinite loop and cannot output results, but theoretically, the program can record the maximum and minimum values among the input numbers (just cannot output, note that the wording in the question is to find and not to output, did not expect that the level one questions would start playing word games).
C: Correct. If scores cannot be -999, the user can terminate the program after inputting by using -999, and the program can correctly output (find) the highest and lowest scores.
D: Claims “It is possible to move cin >> now_num; below while (now_num != -999) { and the result will remain unchanged“. If this modification is made, the code will become:
min_num = max_num = now_num; // now_num is uninitialized, leading to undefined behaviorwhile (now_num != -999) { cin >> now_num; if (max_num < now_num) max_num = now_num; if (min_num > now_num) min_num = now_num;}
Incorrect point:
The loop will first process the initial now_num (value unknown), then read the new value. When inputting -999, -999 will be processed and may update min_num and max_num , which differs from the original code (-999 is not processed), thus the result will change.
Therefore, the answer to this question is D.
【Knowledge Expansion】
Loop Input Structure: When handling sentinel values (like -999), a common pattern is “input first, then loop“, ensuring the sentinel value is not processed. Modifying the input statement position may disrupt this logic.
Variable Initialization: In C++, using uninitialized variables can lead to undefined behavior, always ensure variables are properly initialized before use.
Code Maintenance: When modifying code, pay attention to logical consistency to avoid introducing errors. For input loops, maintaining the original structure is usually safer.