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 ...
随机推荐
- 第33届 MPD软件工作坊(南京站)有哪些亮点值得我们参加?
MPD软件工作坊由msup2010年创办,自创办以来,共吸引了万名的软件从业者到场参与.第33届 MPD软件工作坊(南京站)将于12月17-18日在南京召开,大会报名平台:活动家! 快捷报名通道:ht ...
- Python学习笔记——基础篇【第六周】——shutil模块
常用模块之shutil 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中,可以部分内容 def c ...
- [河南省ACM省赛-第三届] 素数 (nyoj 169)
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #i ...
- Objective-C Runtime 运行时之二:成员变量与属性(转载)
在前面一篇文章中,我们介绍了Runtime中与类和对象相关的内容,从这章开始,我们将讨论类实现细节相关的内容,主要包括类中成员变量,属性,方法,协议与分类的实现. 本章的主要内容将聚集在Runtime ...
- 关于ajax跨域问题
什么是跨域 1.document.domain+iframe的设置 2.动态创建script 3.利用iframe和location.hash 4.window.name实现的跨域数据传输 5.使用H ...
- php 分析
php code in D:\10\11\php test in D:\10\11\php\test issue 1: <html><head><title>标记 ...
- Ubuntu14.04安装完全分布式Hadoop1.2.1
一直想装hadoop集群,但总是没有时间,最近抽了三天时间下定决定装了一下,在我的wmware上虚拟了一台Ubuntu14.04,然后再复制了两台虚拟机,准备开始!!!! 基本参考 http://ww ...
- System.Diagnostics.Process 执行.EXE
分类: C#+WINFORM 2009-04-05 21:09 459人阅读 评论(0) 收藏 举报 我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打 ...
- 查询被收录页面中的死链接 By SEO
朋友说他的站挂了,想知道被收录的页面有多少是死链,于是我就想了一下流程,从Site获得收录数量当然是不精准的,不过也没有更好的地了,真实的收录只有搜索引擎数据库里面才有... 查询被收录页面的状态码, ...
- javascript 中的call和apply
一.作用及应用场景 call和apply是Function的方法,他的第一个参数是this,第二个是Function的参数.call 和 apply 都是为了改变某个函数运行时的 context 即上 ...