在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法。请你编程序计算出共有多少种兑法。
Input
每行只有一个正整数N,N小于32768。
Output
对应每个输入,输出兑换方法数。
Sample Input
2934
12553
Sample Output
718831
13137761
背包问题方案数,有点像爬楼梯问题
代码:
#include <cstdio> #include <algorithm> using namespace std; const int MAXN=40000; long long f[MAXN]; int main(void){ int a[]={1,2,3}; int n; f[0]=1; for(int i=0;i<3;i++){ for(int j=a[i];j<MAXN;j++){ f[j]+=f[j-a[i]]; } } while(~scanf("%d",&n)){ printf("%lld\n",f[n]); } return 0; }
文章来源: hdu1284――钱币兑换问题