Analysis of past CSP-J preliminary competition questions | 2021 CSP-J preliminary competition improvement procedures (34-38)

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 past CSP-J preliminary test questions | Summary_csp past past questions_Blog of a communicator who loves programming-CSDN blog


#include <iostream>

using namespace std;

const int MAXN = 1000000;
int F[MAXN];

int main() {
    int n;
    cin >> n;
    int i = 0, p = 0, c = 0;  //i是人的编号,c是已出队的人数,p表示当前要报的数
    while (①) {
        if (F[i] == 0) {  //表示i在队中
            if (②) {  //报了1
                F[i] = 1;  //i出队
                ③;
            }
            ④;
        }
        ⑤;
    }
    int ans = -1;
    for (i=0; i<n; i++)
        if (F[i] == 0)  //编号为i这个人在队中
            ans = i;
    cout << ans << endl;
    return 0;
}

34. ① should be filled in ( )

A.i<n

B.c<n

C.i<n-1

D.c<n-1

[Answer]: D

【Analysis】

The question requires that there is only one person left. In other words, if there is more than one person left, the cycle will continue. Therefore, if c, which is the number of people leaving the team, is less than n-1 people, the cycle will continue. Choose D

35. ② should be filled in ( )

A.i % 2 == 0

B.i % 2 == 1

C.p

D.!p

[Answer]: C

【Analysis】

If you report the number 1, you will leave the queue, and p represents the current number to be reported, so choose C

36. ③ should be filled in ( )

A.i++

B.i = (i + 1) % n

C.c++

D.p ^= 1

[Answer]: C

【Analysis】

The previous line indicates leaving the team, so this should be the number of people leaving the team + 1, so choose C

37. ④ should be filled in ( )

A.i++

B.i = (i + 1) % n

C.c++

D.p ^= 1

[Answer]: D

【Analysis】

After reporting the number, the next number to be reported should be processed. Only option D operates on p (the number to be reported). 1^1=0, 0^1=1, choose D

38. ⑤ should be filled in ( )

A.i++

B.i = (i + 1) % n

C.c++

D.p ^= 1

[Answer]: B

【Analysis】

This is to process the next person i who wants to count. After i reaches n-1, adding 1 needs to be changed to 0 through mod operation. So choose B

Guess you like

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