'commit'
This commit is contained in:
40
TangDou/LuoGuBook/P5743.cpp
Normal file
40
TangDou/LuoGuBook/P5743.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user