Cash Machine

Problem Description
A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example, 
N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10 
means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each. 
Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine. 
Notes:  @ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc. 
 
Input
The program input is from standard input. Each data set in the input stands for a particular transaction and has the format: 
cash N n1 D1 n2 D2 ... nN DN 
where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers in the input. The input data are correct. 
 
Output
For each set of data the program prints the result to the standard output on a separate line as shown in the examples below. 
 
Sample Input
735 3 4 125 6 5 3 350
633 4 500 30 6 100 1 5 0 1
735 0
0 3 10 100 10 50 10 10
 
Sample Output
735
630
0
0
 
Source
PKU
 

题意:在存款机里有几种不同面值的货币,各有自己的数量。给定一个取款值。

   求出小等于这个值的能取到的最大的数

AC代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<vector>
 6 #include<cmath>
 7 #include<set>
 8 #include<cstdlib>
 9 #include<cstring>
10 #include<stack>
11 #include<string>
12 
13 using namespace std;
14 
15 
16 
17 int n;
18 int cash;
19 struct my//建立结构体,存储每种货币的面值和张数;
20 {
21     int num;
22     int money;
23 
24 } go[11];
25 bool dp[111111];//建立辅助数组;
26 
27 bool cmp(my a,my b)
28 {
29     return a.money<b.money;
30 
31 }//sort的比较函数;以面值的大小为排序依据,从大到小排序;
32 int main()
33 {
34     freopen("1.txt","r",stdin);
35     int i,j,k;
36     while (cin>>cash)//录入最大的金额;
37     {
38         cin>>n;//录入钱币的种数;
39         for (i=0; i<n; i++) //录入张数和面值
40             cin>>go[i].num>>go[i].money;
41         sort(go,go+n,cmp);//进行排序
42         for (i=1; i<=cash; i++)
43             dp[i]=false;
44         dp[0]=true;
45         int big=0;//存储最大能取到的金额;
46         for (k=0;k<n;k++)
47         {
48             for (j=big;j>=0;j--)
49                 if (dp[j])//如果j金额可以取到
50                 {
51                     for (i=1; i<=go[k].num; i++)
52                     {
53                         int cur=j+i*go[k].money;
54                         if (cur>cash||dp[cur])//大于cash的数据不需要;扫过的金额不在进行操作;
55                             break;
56                         dp[cur]=true;
57                         big=max(big,cur);//跟新big;
58                     }
59                 }
60         }
61         for (i=cash; i>=0; i--) if (dp[i])
62             {
63                 cout<<i<<endl;
64                 break;
65             }
66     }
67 }
View Code
原文地址:https://www.cnblogs.com/zhangchengbing/p/3234381.html