Analysis of past CSP-J preliminary test questions | 2021 CSP-J preliminary test reading program (16-21)

Learn C++ from a baby! Record the questions in the process of CSP-J preparation and study, and record every moment.

Attached is a summary post: Analysis of the real questions of the CSP-J preliminary competition over the years | Summary


#include <iostream>
using namespace std;

int n;
int a[1000];

int f(int x)  //统计x转为二进制后1的个数
{
    int ret = 0;
    for (; x; x &= x - 1) ret++;
    return ret;
}

int g(int x)  //获取x转为二进制后1的最低位
{
    return x & -x;
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++)
        cout << f(a[i]) + g(a[i]) << ' ';
    cout << endl;
    return 0;
}

16. When the input n is equal to 1001, the program will not cause subscript out of bounds. ( )

[Answer]: Wrong

【Analysis】

The subscript range of a[1000] is a[0] - a[999], so a[1000] will cause out of bounds

17. The input a[i] must be all positive integers, otherwise the program will fall into an infinite loop. ( )

[Answer]: Wrong

【Analysis】

Try to bring a negative number into the operation, the binary operation logic of the f function does not change, and it will not fall into an infinite loop. In addition, question 21 has negative input

18. When the input is "5 2 11 9 16 10", the output is "3 4 3 17 5". ( )

[Answer]: Wrong

【Analysis】

After inputting the data, calculate it one by one, and the output is 3 4 3 17 4

19. When the input is "1 511998", the output is "18". ( )

[Answer]: Yes

【Analysis】

511998 converted to binary is 0111110011111111110, the calculated result is 18, so it is correct

20. Move the definition of the g function (lines 14-17) in the source code to the back of the main function, and the program can be compiled and run normally. ( )

[Answer]: Wrong

【Analysis】

When running the main function, if the g function is not declared, it cannot be called

21. When the input is "2 -65536 2147483647", the output is ( ).

A.“65532 33”

B.“65552 32”

C.“65535 34”

D.“65554 33”

[Answer]: B

【Analysis】

After converting 2147483647 to binary, it is 2^32-1, that is, there are 31 1s, so the output is 32, choose B

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132739222