题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1284
钱币兑换问题
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9287 Accepted Submission(s): 5665
Problem Description
在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法。请你编程序计算出共有多少种兑法。
Input
每行只有一个正整数N,N小于32768。
Output
对应每个输入,输出兑换方法数。
Sample Input
2934 12553
Sample Output
718831 13137761
Author
SmallBeer(CML)
Source
Recommend
lcy
思路:简单的完全背包问题。只不过这里是求方案数,而不是求价值。因此,预处理出所有结果,然后输入输出即可。
附上AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1<<15;
int dp[maxn];
int n;
int main(){
dp[0] = 1;
for (int i=1; i<=3; ++i)
for (int j=i; j<maxn; ++j)
dp[j] += dp[j-i];
while (~scanf("%d", &n))
printf("%d\n", dp[n]);
return 0;
}