钱币兑换问题
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10390 Accepted Submission(s): 6324
Problem Description
在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法。请你编程序计算出共有多少种兑法。
Input
每行只有一个正整数N,N小于32768。
Output
对应每个输入,输出兑换方法数。
Sample Input
2934 12553
Sample Output
718831 13137761
Author
SmallBeer(CML)
Source
法一:
今天刚学的计数DP。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define ll long long
const int N = 34768+6; //N=33000时wa,有谁知道为什么吗?
int main()
{
ll dp[34768+5];
int val[4];
dp[0]=1;
for(int i=1; i<=3; i++)
{
for(int p=i; p<=34768; p++)
{
dp[p]+=dp[p-i];
}
}
int n;
while(~scanf("%d",&n))
{
printf("%lld\n",dp[n]);
}
return 0;
}
网上看得,遍历3分的,然后2分,剩余1分补齐。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
#define ll long long
const int inf = 0x3f3f3f3f;
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
int a3=n/3;//3分最多有a3
int ans=0;
for(int i=0;i<=a3;i++) //3分拿0-a3个。
{
int a2=(n-i*3)/2;
ans+=a2+1;
}
printf("%d\n",ans);
}
return 0;
}