洛谷-P5743 【深基7.习8】猴子吃桃

洛谷-P5743 【深基7.习8】猴子吃桃

原题链接:https://www.luogu.com.cn/problem/P5743


题目描述

一只小猴买了若干个桃子。第一天他刚好吃了这些桃子的一半,又贪嘴多吃了一个;接下来的每一天它都会吃剩余的桃子的一半外加一个。第 \(n(n\le20)\) 天早上起来一看,只剩下 1 个桃子了。请问小猴买了几个桃子?

输入格式

输出格式

输入输出样例

输入 #1

4

输出 #1

22

C++代码

#include <iostream>
using namespace std;

int main() {
    int n, ans = 1;
    cin >> n;
    for (int i=1; i<n; ++i)
        ans = (ans + 1) * 2;
    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/yuzec/p/13401898.html