http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83php
Time limit: 3.000 secondshtml
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.ios
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).ide
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.spa
0.20 2.00 0.00
0.20 4 2.00 293
分析:code
这个题跟UVA674题相似,只是这个题有更多的钱的种类,而且须要用long long来保存htm
作的方法是先乘以100消除浮点数的偏差,而后计算,须要注意的是输出格式有要求,有特殊的空格要求~blog
AC代码:ip
递推:ci
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 const int maxn=30001; 6 int num[12]={0,5,10,20,50,100,200,500,1000,2000,5000,10000}; 7 int a,b; 8 long long f[maxn][12]; 9 void Init() 10 { 11 for(int i=0;i<12;i++) 12 f[0][i]=1; 13 for(int i=1;i<maxn;i++) 14 for(int j=1;j<12;j++) 15 for(int k=0;num[j]*k<=i;k++) 16 f[i][j]+=f[i-num[j]*k][j-1]; 17 } 18 int main() 19 { 20 Init(); 21 while(scanf("%d.%d",&a,&b)&&(a+b)) 22 { 23 int n=a*100+b; 24 printf("%6.2lf%17lld\n",n*1.0/100,f[n][11]); 25 } 26 return 0; 27 }
背包:
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 const int maxn=30001; 6 int num[12]={0,5,10,20,50,100,200,500,1000,2000,5000,10000}; 7 int a,b; 8 long long f[maxn][12]; 9 void Init() 10 { 11 for(int i=0;i<12;i++) 12 f[0][i]=1; 13 for(int i=1;i<maxn;i++) 14 for(int j=1;j<12;j++) 15 for(int k=0;num[j]*k<=i;k++) 16 f[i][j]+=f[i-num[j]*k][j-1]; 17 } 18 int main() 19 { 20 Init(); 21 while(scanf("%d.%d",&a,&b)&&(a+b)) 22 { 23 int n=a*100+b; 24 printf("%6.2lf%17lld\n",n*1.0/100,f[n][11]); 25 } 26 return 0; 27 }
后来想到能够再除以5(题目说是5的倍数),来减小运算量,提升运算效率。
1 #include <cstdio> 2 using namespace std; 3 4 long long f[30010]; 5 const int num[] = {2000, 1000, 400, 200, 100, 40, 20, 10, 4, 2, 1}; 6 7 int main() 8 { 9 f[0] = 1; 10 for(int i = 0 ;i <= 10;i ++) 11 for(int j = num[i] ;j <= 6005;j ++) 12 f[j] += f[j - num[i]]; 13 double x; 14 while(scanf("%lf", &x) , x) 15 { 16 double tx = x * 20; 17 printf("%6.2lf%17lld\n", x , f[(int)tx] ); 18 } 19 return 0; 20 }