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 ...
随机推荐
- java学习记录
1,接口(不实现任何方法)——>抽象类(实现部分公共方法)——>简单实现类——>具体实现类 2,抽象类不能被直接实例化,只能实现抽象方法,以匿名内部类的方式表现. 3,如果stati ...
- C++友元
通过friend关键字,我们可以将不属于当前类的一个函数在当前类中加以声明,该函数便可以成为当前类的友元函数. 例1: #include<iostream>using namespace ...
- js常用语句写法
1.for语句 for(var i = 0; i<6; i++) //0,1,2,3,4,5
- Codeforces Round #346 (Div. 2) D Bicycle Race
D. Bicycle Race 题目链接http://codeforces.com/contest/659/problem/D Description Maria participates in a ...
- sqlserver 更改跟踪相关知识
数据捕获相关文章: http://www.cnblogs.com/lyhabc/p/3383484.html http://www.cnblogs.com/chenmh/p/4408825.html ...
- Python 修炼2
Python开发IDE:Pycharm.elipse 1.运算符 1 1.算数运算 + - * / // ** % 2. 赋值运算 a = 1 a += 2 3.比较运算 1>3 4.逻辑运算 ...
- APICloud界面间跳转
UZModule提供属性controller,可通过该控制器对目标控制器进行push或者present操作. // push[self.viewController.navigationControl ...
- C# 从零开始 vol.2
这是第二篇 1:命名空间 命名空间可以理解成类的文件夹,这个命名空间中存放着各种类,当你需要使用到对应的类的时候,就需要导入命名空间后才能使用. 引用:可以理解成添加新的存放类的文件夹,也就是一个项目 ...
- mysql索引类型-形式-使用时机-不足之处--注意事项
一.索引的类型 1.普通索引 增加 create index index_name on table(colume(length)); 例子:cre ...
- neutron openvswitch agent实现安全组的方法
关于openstack安全组,采用一问一答的形式记录如下 1. 是加载在计算节点的还是网络节点的? 是加载在计算节点的 2. 是使用iptable规则实现的吗? M版的neutron实现了openvs ...