贪心-HDU3348 coins(钱币问题)
文章目录
- 题目
- 题意
- 分析
- 代码
题目
传送门: HDU-3348
“Yakexi, this is the best age!” Dong MW works hard and get high pay, he has many 1 Jiao and 5 Jiao banknotes(纸币), some day he went to a bank and changes part of his money into 1 Yuan, 5 Yuan, 10 Yuan.(1 Yuan = 10 Jiao) “Thanks to the best age, I can buy many things!” Now Dong MW has a book to buy, it costs P Jiao. He wonders how many banknotes at least,and how many banknotes at most he can use to buy this nice book. Dong MW is a bit strange, he doesn’t like to get the change, that is, he will give the bookseller exactly P Jiao.
input:
T(T<=100) in the first line, indicating the case number. T lines with 6 integers each: P a1 a5 a10 a50 a100 ai means number of i-Jiao banknotes. All integers are smaller than 1000000.
output:
Two integers A,B for each case, A is the fewest number of banknotes to buy the book exactly, and B is the largest number to buy exactly.If Dong MW can’t buy the book with no change, output “-1 -1”.
Sample Input:
3
33 6 6 6 6 6
10 10 10 10 10 10
11 0 1 20 20 20
Sample Output:
6 9
1 10
-1 -1
题意
分别给出1,5,10,50,100角面值的纸币张数,求凑出p角的最小张数和最大张数。
分析
- 最小:每次贪心选择面值最大的纸币来凑成p角,凑不成则输出-1。
- 最大:用对立反面来求,用最小张数凑成它的反面,那么剩余张数就是凑成p的最大张数。设所有纸币组成总价值sum和纸币总数cnt,用上一点求凑成(sum-p)最小张数x,cnt-x即可。
代码
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1000006;
int t, p, a[5], b[5] = { 1,5,10,50,100 };
int mi, ma, sum, cnt, fp, ti;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d%d%d", &p, &a[0], &a[1], &a[2], &a[3], &a[4]);
mi = ma = sum = cnt = 0;
for (int i = 0; i <= 4; i++) {
sum += b[i] * a[i];//纸币总价值
cnt += a[i];//纸币总数
}
fp = sum - p;
for (int i = 4; i >= 0; i--) {
ti = p / b[i];//最多换b[i]面值几张
if (ti > a[i])ti = a[i];//超过题目输入张数
mi += ti;
p -= ti * b[i];
}
if (p) {
printf("-1 -1n");
continue;
}
for (int i = 4; i >= 0; i--) {
ti = fp / b[i];
if (ti > a[i])ti = a[i];
ma += ti;
fp -= ti * b[i];
}
printf("%d %dn", mi, cnt - ma);
}
return 0;
}
原创不易,请勿转载(
本不富裕的访问量雪上加霜) 博主首页:https://blog.csdn.net/qq_45034708
- Golang语言社区--模板的使用
- Golang语言写Web 应用程序
- 强制wordpress后台HTTPS
- Golang语言 Cookie的使用
- Golang 语言调用动态库实现OpenGL及windows的API编程
- MySQL中的Online DDL(第一篇)(r11笔记第3天)
- 转--quick-cocos做客户端,golang做服务端,实现HTTP通信
- Nginx配置SSL证书
- Golang语言RPC Authorization进行简单ip安全验证的方法
- 深入理解Oracle中的DBCA
- Golang语言goto语句
- 转--Golang语言语法汇总
- Oracle,MySQL迁移整合的问题总结(r10笔记第99天)
- MySQL修复表的简单分析(r11笔记第19天)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 《内蒙古自治区第十二届大学生程序设计竞赛试题_G: 最大收益》
- Vue 实现前进刷新,后退不刷新的效果
- 2015 09 CCF计算机软件能力认证试题第四题高速公路
- 《hdu 4540 威威猫打地鼠》
- 多个请求下 loading 的展示与关闭
- 第13期:表统计信息的计算
- 2019.8.15乘兴打Codeforces Round #569 (Div. 2)小记A题A. Alex and a Rhombus
- 2019.8.15乘兴打Codeforces Round #569 (Div. 2)小记B. Nick and Array
- 《hdu 免费馅饼》
- 技术分享 | 使用 pt-query-digest 分析慢日志
- 2019.8.15乘兴打Codeforces Round #569 (Div. 2)小记
- Codeforces Beta Round #14 (Div. 2)A. Letter
- Vue 改变数据,页面不刷新的问题
- R语言再保险合同定价案例研究
- 开发一个简单的 Vue 弹窗组件