UVA 147

来源:互联网 发布:源码安装php选项 编辑:程序博客网 时间:2024/05/13 13:59

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_inline25 10c, 10c+2 tex2html_wrap_inline25 5c, and 4 tex2html_wrap_inline25 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.202.000.00

Sample output

  0.20                4  2.00              293




题目大意:

给定11种面值分别为$100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 10c and 5c coins的钱,现在给定一个钱数,求出可以组成的种类数,类似于uva 674,不过此题的精度太强了


int n=(int)(nn*100+0.5);,注意用long long ,种类数可能非常大

用dp[i][j]表示用前i种硬币,组成j分的种类数,

状态转移方程:dp[i][j]+=DP(i-1,j-k*d[i]);


AC代码: cop

  1. #include<stdio.h>  
  2. #define N 30005  
  3. #include<string.h>  
  4. #define ll long long  
  5. int d[11]= {5,10,20,50,100,200,500,1000,2000,5000,10000};  
  6. ll dp[15][N];//dp[i][j]表示用前i种硬币,组成j分的种类数  
  7. ll DP(ll i,ll j)  
  8. {  
  9.     //printf("%d %d %d\n",i,j,dp[i][j]);  
  10.     if(dp[i][j]!=-1)  
  11.         return dp[i][j];  
  12.     dp[i][j]=0;  
  13.     for(int k=0;j-k*d[i]>=0;k++)  
  14.     {  
  15.         dp[i][j]+=DP(i-1,j-k*d[i]);  
  16.     }  
  17.     return dp[i][j];  
  18. }  
  19. int main()  
  20. {  
  21.     double nn;  
  22.     memset(dp,-1,sizeof(dp));//赋值一次即可,否则可能会超时  
  23.     while(scanf("%lf",&nn)!=EOF)  
  24.     {  
  25.         if(nn==0.00)  
  26.             break;  
  27.         int n=(int)(nn*100+0.5);//注意精度  
  28.         for(ll i=0; i<=n; i++)  
  29.             dp[0][i]=1;  
  30.             printf("%6.2f%17lld\n",nn,DP(10,n));  
  31.     }  
  32.     return 0;  
  33. }  




递推代码:

[cpp] view plain copy
 print?
  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. }  

背包代码:

[cpp] view plain copy
 print?
  1. #include<cstdio>  
  2. #include<cstring>  
  3. #include<iostream>  
  4. using namespace std;  
  5. const int maxn=30005;  
  6. int a,b,num[12]={5,10,20,50,100,200,500,1000,2000,5000,10000};  
  7. long long f[maxn];  
  8. void Init()  
  9. {  
  10.     f[0]=1;  
  11.     for(int i=0;i<11;i++)  
  12.     for(int j=num[i];j<maxn;j++)  
  13.         f[j]+=f[j-num[i]];  
  14. }  
  15. int main()  
  16. {  
  17.     Init();  
  18.     while(scanf("%d.%d",&a,&b)&&(a+b))  
  19.     {  
  20.     int n=a*100+b;  
  21.     double ans=n*1.0/100.0;  
  22.     printf("%6.2lf%17lld\n",ans,f[n]);  
  23.     }  
  24.     return 0;  
  25. }  


递推代码:

[cpp] view plain copy
 print?
  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. }  

背包代码:

[cpp] view plain copy
 print?
  1. #include<cstdio>  
  2. #include<cstring>  
  3. #include<iostream>  
  4. using namespace std;  
  5. const int maxn=30005;  
  6. int a,b,num[12]={5,10,20,50,100,200,500,1000,2000,5000,10000};  
  7. long long f[maxn];  
  8. void Init()  
  9. {  
  10.     f[0]=1;  
  11.     for(int i=0;i<11;i++)  
  12.     for(int j=num[i];j<maxn;j++)  
  13.         f[j]+=f[j-num[i]];  
  14. }  
  15. int main()  
  16. {  
  17.     Init();  
  18.     while(scanf("%d.%d",&a,&b)&&(a+b))  
  19.     {  
  20.     int n=a*100+b;  
  21.     double ans=n*1.0/100.0;  
  22.     printf("%6.2lf%17lld\n",ans,f[n]);  
  23.     }  
  24.     return 0;  
  25. }  
0 0
原创粉丝点击