1、钱币兑换问题
在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法。请你编程序计算出共有多少种兑法。
Input
每行只有一个正整数N,N小于32768。
Output
对应每个输入,输出兑换方法数。
Sample Input
2934
12553
Sample Output
718831
13137761
这种题不需要选择放或不放,与递推相似,一般是采用打表的方法,是一种完全背包
#include<stdio.h>
int main()
{
int a[50000];
a[0]=1;
for(int i=1;i<=3;i++)
{
for(int j=i;j<=35000;j++)
{
a[j]+=a[j-i];
}
}
int n;
while(~scanf("%d",&n))
{
printf("%d\n",a[n]);
}
}
2、Dollars
New Zealand currency consists of $100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 10c and 5c
coins. Write a program that will determine, for any given amount, in how many ways that amount
may be made up. Changing the order of listing does not increase the count. Thus 20c may be made
up in 4 ways: 1×20c, 2×10c, 10c+2×5c, and 4×5c.
Input
Input will consist of a series of real numbers no greater than $300.00 each on a separate line. Each
amount will be valid, that is will be a multiple of 5c. The file will be terminated by a line containing
zero (0.00).
Output
Output will consist of a line for each of the amounts in the input, each line consisting of the amount
of money (with two decimal places and right justified in a field of width 6), followed by the number of
ways in which that amount may be made up, right justified in a field of width 17.
Sample Input
0.20
2.00
0.00
Sample Output
0.20 4
2.00 293
大意是有5,10,20,50分以及1,2,5,10,20,50,100元这几种面额,输入一个面额,求组成它有几种方法:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int b[11]= {5,10,20,50,100,200,500,1000,2000,5000,10000};
long long dp[35000];
int main()
{
double kk;
dp[0]=1;
for(int i=0; i<11; i++)
{
for(int j=5; j<=30000; j+=5) //与上一个不同,这里是j+=5,因为最小面值是5
{
if(j>=b[i])
dp[j]+=dp[j-b[i]];
}
}
while(~scanf("%lf",&kk))
{
int sum=0;
if(kk==0.00)
break;
int k=kk*100+0.5; //这里是小数转化为整数的精度问题,可自行百度
printf("%6.2lf%17lld\n",kk,dp[k]);
}
}