Files
python/TangDou/LuoGuBook/P5743.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

40 lines
969 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n;
/**
思路分析:
1、比如第10天桃子数量为1,我们记为s10=1.
2、那么我们可以考虑s9是多少?
3、第9天时,吃掉了s9的一半,再加1个,然后变成的s10.就是 吃掉了:s9/2+1,剩下: s9-s9/2-1=s9/2-1=s10
4、整理得到s9=2s10+2
5、通项公式为 Si=2*S(i+1)+2
6、还知道Sn=1
*/
/**
* 递归函数的定义及参数理解是最重要的!
* @param n 第几天
* @return x天剩余多少桃子
*/
int dfs(int x) {
if (x == n) return 1;
return 2 * dfs(x + 1) + 2;
}
int main() {
cin >> n;
//方法1:逆推
LL ans = 1; //最后一天剩下一个
for (int i = n - 1; i >= 1; i--) ans = ans * 2 + 2;
cout << ans << endl;
//方法2:递归法 顺推
cout << dfs(1) << endl;
//**不管是怎么推,都是要整理出通项公式**
return 0;
}