HDU1284 钱币兑换问题

在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法。请你编程序计算出共有多少种兑法。

Input

每行只有一个正整数N,N小于32768。

Output

对应每个输入,输出兑换方法数。

Sample Input

2934
12553

Sample Output

718831
13137761

总结:

法一:类似于爬楼梯的问题。递推问题,先首先考虑全1分情况,然后加入2分从dp[i-1]递推。最后加入3分。递推公式是d[j] = d[j] + d[j-i]。分别递推到d[n].

法二:数学方法;

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 32800;
long long dp[MAX];

int main()
{
	int n;
	
	while (scanf_s("%d", &n) != EOF)
	{
		memset(dp, 0, sizeof(dp));
		int a[3] = { 1,2,3 };
		dp[0] = 1;
		for (int i = 1; i <= 3; ++i)
		{
			for (int j = i; j <= 32800; ++j)
			{
				dp[j]+=dp[j-i];
			}
		}
		printf("%d\n", dp[n]);
	}
    return 0;
	
}

猜你喜欢

转载自blog.csdn.net/fighting_yifeng/article/details/81292854
今日推荐