1 条题解

  • 0
    @ 2090-7-22 9:29:22

    【深基7.习8】猴子吃桃

    解题思路

    从第 n 天早上的 1 个桃子倒推。如果第二天剩 y 个,那么前一天有 2×(y+1) 个。倒推 n-1 次。

    正确性说明

    前一天 x 个吃掉 x/2+1 个后剩 x/2-1=y,解得 x=2(y+1)。每次逆运算都恢复前一天数量。

    易错点

    第 n 天早上尚未吃桃,只倒推 n-1 次;n=1 时答案就是 1。

    复杂度

    时间 O(n),空间 O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        cin >> n;
        long long ans = 1;
        for (int i = 1; i < n; i++)
            ans = 2 * (ans + 1);
        cout << ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    18
    时间
    1000ms
    内存
    125MiB
    难度
    进阶
    标签
    递交数
    1
    已通过
    1
    上传者