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代码:

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<string> using namespace std; int n;
int cash;
struct my//建立结构体,存储每种货币的面值和张数;
{
int num;
int money; } go[];
bool dp[];//建立辅助数组; bool cmp(my a,my b)
{
return a.money<b.money; }//sort的比较函数;以面值的大小为排序依据,从大到小排序;
int main()
{
freopen("1.txt","r",stdin);
int i,j,k;
while (cin>>cash)//录入最大的金额;
{
cin>>n;//录入钱币的种数;
for (i=; i<n; i++) //录入张数和面值
cin>>go[i].num>>go[i].money;
sort(go,go+n,cmp);//进行排序
for (i=; i<=cash; i++)
dp[i]=false;
dp[]=true;
int big=;//存储最大能取到的金额;
for (k=;k<n;k++)
{
for (j=big;j>=;j--)
if (dp[j])//如果j金额可以取到
{
for (i=; i<=go[k].num; i++)
{
int cur=j+i*go[k].money;
if (cur>cash||dp[cur])//大于cash的数据不需要;扫过的金额不在进行操作;
break;
dp[cur]=true;
big=max(big,cur);//跟新big;
}
}
}
for (i=cash; i>=; i--) if (dp[i])
{
cout<<i<<endl;
break;
}
}
}

Cash Machine的更多相关文章

  1. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  2. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

  3. POJ 1276 Cash Machine

    Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24213 Accepted: 8476 Descrip ...

  4. PKU 1276 Cash Machine

    <span style="color:#000099;">/* Cash Machine Time Limit: 1000MS Memory Limit: 10000K ...

  5. Cash Machine(多重背包二进制转换)

    个人心得:多重背包,自己根据转换方程写总是TLE,后面去网上看了二进制转换,不太理解: 后面仔细想了下,用自己的思想理解下把,就是将对应number,cash总和用二进制拆分, 然后全部装入到一个数组 ...

  6. POJ 1276 Cash Machine(单调队列优化多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38986   Accepted: 14186 De ...

  7. POJ 1276:Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30006   Accepted: 10811 De ...

  8. POJ 1276 Cash Machine -- 动态规划(背包问题)

    题目地址:http://poj.org/problem?id=1276 Description A Bank plans to install a machine for cash withdrawa ...

  9. Cash Machine(多重背包)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24067   Accepted: 8414 Description A Ba ...

  10. POJ1276:Cash Machine(多重背包)

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

随机推荐

  1. Java NIO Channel之FileChannel [ 转载 ]

    Java NIO Channel之FileChannel [ 转载 ] @author zachary.guo 对于文件 I/O,最强大之处在于异步 I/O(asynchronous I/O),它允许 ...

  2. Postman怎么用?

    国庆期间的工作就是搞清楚postman怎么用?

  3. Nginx redirect

    if ($host != 'www.xxxxx.com' ) { rewrite ^/(.*)$ http://www.xxxx.com/$1 permanent; }

  4. EBS FORM FOLDER 开发,单元格无法使用右键

    问题描述: 在使用folder开发FORM后,单元格无法使用右键,正常应该可以右键进行隐藏.显示.复制等操作. 通过对比发现是因ITEM属性中 弹出式菜单未设置导致. 解决方法: 设置弹出式菜单

  5. php添加扩展插件

    给PHP安装扩展的方式有好多 一.重新编译 进入PHP源码目录./configure --prefix=/usr/local/php ...[其他编译参数] 二.通过phpize添加扩展 进入PHP源 ...

  6. C3P0连接池参数详解

    <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> < ...

  7. mysql 提示too many connections”的解决办法

    最近使用python多线程连接mysq打数据,安装好mysql后,使用500线程连接发现提示:too many connections, 查询方法得知是需要进行配置才行: 产生这种问题的原因是: 连接 ...

  8. C++线程安全的单例模式

    1.在GCC4.0之后的环境下: #include <iostream> using namespace std;template <typename T>class Sing ...

  9. jQuery(5)——动画

    jQuery中的动画 [show()方法和hide()方法] 在HTML文档中,为一个元素调用hide()方法,会将该元素的display样式改为“none”,show()方法将元素的display样 ...

  10. FR javascript 时间设置上个月最后一天后当月最后一天

    //设置上月最后一天 var date1 =new Date(); date1.setDate(1); //first day; date1.setMonth(date1.getMonth()); / ...