Cash Machine
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.
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.
题意:在存款机里有几种不同面值的货币,各有自己的数量。给定一个取款值。
求出小等于这个值的能取到的最大的数
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的更多相关文章
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- poj 1276 Cash Machine(多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33444 Accepted: 12106 De ...
- POJ 1276 Cash Machine
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24213 Accepted: 8476 Descrip ...
- PKU 1276 Cash Machine
<span style="color:#000099;">/* Cash Machine Time Limit: 1000MS Memory Limit: 10000K ...
- Cash Machine(多重背包二进制转换)
个人心得:多重背包,自己根据转换方程写总是TLE,后面去网上看了二进制转换,不太理解: 后面仔细想了下,用自己的思想理解下把,就是将对应number,cash总和用二进制拆分, 然后全部装入到一个数组 ...
- POJ 1276 Cash Machine(单调队列优化多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38986 Accepted: 14186 De ...
- POJ 1276:Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30006 Accepted: 10811 De ...
- POJ 1276 Cash Machine -- 动态规划(背包问题)
题目地址:http://poj.org/problem?id=1276 Description A Bank plans to install a machine for cash withdrawa ...
- Cash Machine(多重背包)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24067 Accepted: 8414 Description A Ba ...
- POJ1276:Cash Machine(多重背包)
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
随机推荐
- Oracle的闪回技术--闪回错误的DML操作
提交DML操作后,该操作使用的还原段就可以被其它对象使用了,为了保证闪回操作时这些数据仍然被保存在还原段中,可能需要重新设置undo_retention参数,表示一个事务提交后,该事务的数据必须保存在 ...
- Python学习笔记——基础篇【第五周】——os模块和sys模块
OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录 ...
- stm32
GPIO NVIC TIME USART ONE WIRE IIC SPI PWM ADC LCD XPT UCOSiii移植 定时器 蓝牙 陀螺仪
- win8.1和centos6.5 双系统启动问题
笔记本系统为centos 6.5,由grub引导启动,安装了win 8.1后,开机直接进入win 8.1,没有出现centos6.5 引导项,解决办法: 一.开机按ESC键进入启动顺序菜单,选择cen ...
- SQLServer 重建索引前后对比
在做维护项目的时,我们经常会遇到索引维护的问题,通过语句,我们就可以判断某个表的索引是否需要重建. 执行一下语句:先分析表的索引 分析表的索引建立情况:DBCC showcontig('Table') ...
- 学习【Web前端开发修炼之道】总结
网页布局 1.申明文档模式 2.设置css标签重置,避免各浏览器解析不同. 3.网页css模块划分,base.css,common.css,page.css 4.低权重原则---避免滥用子选择器 cs ...
- Mybatis的传参
最近重新温习了遍Mybatis ,觉得还是汇总一下比较好,方便自己以后的快速开发 最终要的一点事,自己写的话,记忆更加深刻: 首先自己先写了个静态块,防止代码冗余: private static Sq ...
- php+mysql 内联接 和 子查询
INNER JOIN(内连接):取得两个表中存在连接匹配关系的记录 $sql="SELECT * FROM subject as a INNER JOIN e_user as b ON a. ...
- angular $parse $eval parse VS eval
$parse angular中重要指令介绍($eval,$parse和$compile) Advanced Angular: $parse $parse ---------------------- ...
- 转: Executor类
Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,Completion ...