Analysis of past CSP-J semi-final exam questions | 2023 T1 Little Apple

Learn C++ from a young age! Record the questions in the CSP-J exam preparation study process and record every moment.

Attached is a summary post: Analysis of past CSP-J semi-final questions | Summary_Blog of a communicator who loves programming-CSDN Blog


[Title description]

On Little Y's table are n apples arranged in a row from left to right, numbered from 1 to n.

Xiaobao is a good friend of Xiao Y. She takes some apples from him every day.

When taking it every day, Xiaobao starts from the first apple on the left and takes out one apple every two apples. Xiaobao will then rearrange the remaining apples into a row in their original order.

Xiaobao wants to know how many days it will take to get all the apples, and on which day the apple numbered n will be taken away?

【enter】

The first line of input contains a positive integer n, representing the total number of apples.

【Output】

The output line contains two positive integers, separated by a space, indicating the number of days it takes for Xiaobao to take away all the apples and the day on which the apple numbered n is taken away.

【Input sample】

8

【Output sample】

5 5

[Detailed code explanation]

#include <bits/stdc++.h>
#include <cmath>
using namespace std;
int n, take = 0, cnt = 1, ans2 = 0;
int main()
{
    cin >> n;
    while (n>0) {
        if (!ans2 && n%3==1) {
            ans2 = cnt;
        }
        take = ceil(1.0*n/3);
        // cout << "n take " << n << " " << take << endl;
        n = n - take;
        cnt++;
    }
    cout << cnt-1 << " " << ans2 << endl;
    return 0;
}

【operation result】

8
5 5

Guess you like

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