uva 147 Dollars

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83php

147 - Dollars

Time limit: 3.000 secondshtml

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 tex2html_wrap_inline25 20c, 2 tex2html_wrap_inline2510c, 10c+2 tex2html_wrap_inline25 5c, and 4 tex2html_wrap_inline25 5c.ios

 

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).ide

 

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.spa

 

Sample input

 

0.20
2.00
0.00

 

Sample output

 

  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 }
View Code

 

 

揹包:

 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 }
View Code

 

後來想到能夠再除以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 }
相關文章
相關標籤/搜索