小P的故事——神奇的换零钱
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
已知A国经济很落后,他们只有1、2、3元三种面值的硬币,有一天小P要去A国旅行,想换一些零钱,小P很想知道将钱N兑换成硬币有很多种兑法,但是可惜的是他的数学竟然是体育老师教的,所以他不会啊、、、他只好求助于你,你可以帮他解决吗?
提示:输入数据大于32000组。
提示:输入数据大于32000组。
Input
每行只有一个正整数N,N小于32768。
Output
对应每个输入,输出兑换方法数。
Example Input
100 1500
Example Output
884 188251
Hint
Author
xfl
#include <stdio.h> int main() { int n,i,j,a[32769] = {0}; a[1] = 1,a[2] = 2,a[3] = 3; for(i = 4;i < 32769;i++) { for(j = 0;j <= i;j+=3) { a[i] += (i-j)/2+1; } } while(scanf("%d",&n) != EOF) { printf("%d\n",a[n]); } return 0; }
正解 多重背包
#include <stdio.h>
#include <string.h>
int main()
{
int n, i, j, c[] = {1, 2, 3};
int dp[40000];
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for(i = 0; i < 3; i++)
{
for(j = c[i]; j <= 32768; j++)
{
dp[j] = dp[j] + dp[j-c[i]];
}
}
while(scanf("%d", &n) != EOF)
{
printf("%d\n", dp[n]);
}
return 0;
}