码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 147(子集和问题)

时间:2015-12-30 22:06:15      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

 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


思路:首先离线用dp求出范围内的所有答案。
    由于最小货币为5分;所以11种货币以5分为计算单位;
    令:
      b[i]为第i种货币含5分硬币的数量(0<=i&&i<11);
      a[i,j]用前i种货币构成j个5分硬币的方案数(0<=i&&i<11,0<=n&&n<=10000).仅用5分硬币所有可能值为a[1,j]=1;
            for(int i=1;i<11;i++)
                for(int j=b[i];j<10000;j++){
                    a[j]+=a[j-b[i]];
               }

    
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<iomanip>
 7 #include<cmath>
 8 #include<vector>
 9 #include<queue>
10 #include<stack>
11 using namespace std;
12 #define PI 3.141592653589792128462643383279502
13 long long a[10000];
14 int b[12]={1,2,4,10,20,40,100,200,400,1000,2000};
15 int main(){
16     //#ifdef CDZSC_June
17     //freopen("in.txt","r",stdin);
18     //#endif
19     //std::ios::sync_with_stdio(false);
20     for(int i=0;i<10000;i++) a[i]=1;
21     for(int i=1;i<11;i++)
22         for(int j=b[i];j<10000;j++){
23             a[j]+=a[j-b[i]];
24         }
25     while(true){
26         double n;
27         scanf("%lf",&n);
28         if(n==0.0) break;
29         int s=int(n*20.0);
30         printf("%6.2lf%17lld\n",n,a[s]);
31     }
32     return 0;
33 }

 

UVA 147(子集和问题)

标签:

原文地址:http://www.cnblogs.com/yoyo-sincerely/p/5090060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!