1 solutions
-
0
解法一:你可以爆搜
#include <bits/stdc++.h> using namespace std; int n, ans; int coin[3]; void dfs(int x, int s) { if (s < 0) return; if (x > 2 * n) { ans++; return; } if (coin[1] < n) { coin[1]++; dfs(x + 1, s + 1); coin[1]--; } if (coin[2] < n) { coin[2]++; dfs(x + 1, s - 1); coin[2]--; } } int main() { cin >> n; dfs(1, 0); cout << ans; return 0; }
解法二: 这道题说白了就一件事:在任意时刻,手握一元钱的同学的钱的数量不得少于手握两元钱的同学的数量。
所以,dp就可以完美解决这个问题(标数法)
详情看代码:
#include <bits/stdc++.h> using namespace std; int dp[20][20]; int n; int main() { cin >> n; dp[1][1] = 1; for (int i = 1; i <= n + 1; i++) { //加一的原因是因为人数可能为由零个人到n个人,是n+1种可能性 for (int j = i; j <= n + 1; j++) { //从i开始是为了防止有两元钱同学超过有一元钱的同学(找不开了) if (!(i == 1 && j == 1)) dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; //转移方程:上一个加一个一元的同学和另外一个加一个两元的同学 } } cout << dp[n + 1][n + 1] << endl; //完美收工 return 0; }
- 1
Information
- ID
- 608
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 7
- Tags
- # Submissions
- 15
- Accepted
- 8
- Uploaded By