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 ...
随机推荐
- Markdown引用本地图片语法
Markdown引用本地图片语法 markdown引用图片标准方式如下:  测试markdown文本如下: # 测试相对路径图片 ![Alt ...
- 用DriverBackUp备份了文件 装好系统后怎么把备份的驱动文件还原
1.打开DriverBackUp 2.菜单栏选择Restore 3.选择open backup file 4.找到备份文件位置,并选择.bki后缀的文件 5.点击"打开" 6.勾选 ...
- MySQL字段联合去重sql
MySQL字段联合去重sql 例如数据 id,name1,name2 1 a x 2 a y 3 b x 4 a y 5 a x 联合去重name1,name2的结果为 id,name1,name2 ...
- Eclipse主题设置
1. 内部编辑区域主题 Eclipse黑色主题包 下载主题包解压到Eclipse安装目录下的dropins目录,重启Eclipse,Windows—>Preferences—>Genera ...
- 织梦dedecms文章发布日期时间调用标签大全
dedecms首页时间标签: 1.12-27 样式 [field:pubdate function='strftime("%m-%d",@me)'/] 2.May 15, 2012 ...
- 一种基于FSIM对视频编码图像质量客观评价的方法
一 为什么对视频编码图像质量客观评价 视频图像质量主观评价一般采用连续双激励质量度量法对任一观测者连续给出原始视频图像和处理过的失真图像,由观测者根据主观感知给出分值,其需针对多个视频对象进行 ...
- 无法启动T-SQL调试。未能连接到计算机"."。这是在主机名解析时通常出现的暂时错误……
今天在按在SSMS中,按F11进行断点调试时,弹出错误:"无法启动T-SQL调试.未能连接到计算器".".这是在主机名解析时通常出现的暂时错误--", 如下图: ...
- PHP编程----猴子选大王
<?php/** * 猴子选大王 * 17个猴子围成一圈,从某个开始报数1-2-3-1-2-3---报"3"的猴子就被淘汰, * 游戏一直进行到圈内只剩一只猴子它就是猴大王了 ...
- 初始化一个本地GIT仓储
简单总结下 // 定位到仓储文件夹目录 $ cd /dir // 初始化本地仓储 $ git init ``` 添加本地GIT忽略清单文件.gitignore```// 添加OS X中系统文件.DS_ ...
- 本地缓存FMDB的使用(iOS)
一.简单说明 1.什么是FMDB FMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API 2.FMDB的优点 使用起来更加面向对象,省去了很多麻烦.冗余的C ...