P9748题解

· · 题解

数学题。

题意:有 n 个苹果,每次操作隔两个拿一个,求操作次数和拿到最后一个苹果的操作是第几次。

因为是隔两个拿一个,所以设苹果数量为 x ,当 x \bmod 3=1 时,最后一个苹果就会被拿走。而每次拿走的苹果数量是 ⌈x/3⌉ 个,简单模拟即可。

考场代码:

#include <bits/stdc++.h>

using namespace std;

int main() {
    //freopen("apple.in", "r", stdin);
    //freopen("apple.out", "w", stdout);
    long long n;
    cin >> n;
    long long ans1 = 0, ans2 = 0;
    bool y = 0;
    long long nn = n;
    if (n % 3 == 1) {
        ans2 = 1;
        y = 1;
    }
    while (nn) {
        ans1++;
        nn -= (nn - 1) / 3 + 1;
        if ((nn - 1) % 3 == 0 && y == 0) {
            y = 1;
            ans2 = ans1 + 1;
        }
    }
    cout << ans1 << ' ' << ans2 << endl;
    return 0;
}