PAT 1048. Find Coins
two sum题目,算是贪婪吧
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm> using namespace std; int main() {
int N, M;
scanf("%d%d", &N, &M); vector<int> coins(N);
for (int i=; i<N; i++) {
scanf("%d", &coins[i]);
} sort(coins.begin(), coins.end()); int p = , q = N - ;
while (p<q) {
int sum = coins[p] + coins[q];
if (sum > M) {
// bigger, so decrease it, move q to the smaller coins
q--;
} else if (sum < M) {
// smaller, so increase it, move p to the bigger coins
p++;
} else {
// we found the coins
printf("%d %d\n", coins[p], coins[q]);
break;
}
}
if (p >= q) {
printf("No Solution\n");
}
return ;
}
PAT 1048. Find Coins的更多相关文章
- PAT 1048 Find Coins[比较]
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...
- PAT 解题报告 1048. Find Coins (25)
1048. Find Coins (25) Eva loves to collect coins from all over the universe, including some other pl ...
- PAT甲 1048. Find Coins (25) 2016-09-09 23:15 29人阅读 评论(0) 收藏
1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...
- PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other ...
- 浙大pat 1048 题解
1048. Find Coins (25) 时间限制 50 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...
- 1048 Find Coins (25 分)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...
- PAT 甲级 1048 Find Coins
https://pintia.cn/problem-sets/994805342720868352/problems/994805432256675840 Eva loves to collect c ...
- PAT Advanced 1048 Find Coins (25 分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- PAT Advanced 1048 Find Coins (25) [Hash散列]
题目 Eva loves to collect coins from all over the universe, including some other planets like Mars. On ...
随机推荐
- js 自定义获得类class和获得id
使用js获取类名,但是低版本浏览器不支持getElementsByClassName,所以我们就需要自定义getClassName,方便跨浏览器使用. 当然,如果采用jquery就不需要. //获取类 ...
- MongoDB mongo.exe启动及闪退解决 转载
转载自:http://blog.csdn.net/wyx_wx/article/details/76108662 启动: 进入MongoDB安装目录下的bin目录,启动mongod.exe 出现如图所 ...
- JavaWeb学习笔记(十六)—— 事务
一.事务概述 1.1 什么是事务 银行转账!张三转10000块到李四的账户,这其实需要两条SQL语句: 给张三的账户减去10000元: 给李四的账户加上10000元. 如果在第一条SQL语句执行成功后 ...
- spring测试save方法报错
用test类测试service的save方法时,报错如下: 2018-08-24 21:52:13,506 - could not read a hi value com.mysql.jdbc.exc ...
- window.load方法 (加载全部图片,第三方网站时使用)
$(window).load( function(){ console.lo ...
- 解决执行maven项目出现 SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. error
最近再弄maven项目,运行起来没有问题,但是Console控制台会报错,比如说如下的问题异常提示: 由此我们可以看出,报出错误的地方主要是slf4j的jar包,而故障码中“Failed to loa ...
- Android中调用高德导航(组件)
btn_.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //调用 ...
- (转)linux shell 数字计算详解
代码中免不了要进行各种数据计算.抛开科学计算不提,普通的计算占地,百分比,同比,环比等需求就很常见.linux shell中进行数字计算,主要有如下几种方式: 1.bc bc是比较常用的linux计算 ...
- 理解restful 架构 && RESTful API设计指南
restful是前端和后端接口中都会使用的设计思想. 网站即软件,我们也常说的webapp,这种互联网软件采用的是“客户端/服务器”模式,建立在分布式体系上. 网站开发,也可以完全采用软件开发的模式, ...
- electron --- 构建桌面应用
最近无意间看到了electron和nw的相关信息,感到很惊讶,因为学习前端也有一段时间了,竟然发现js还有这么强大的功能,因为js不仅可以写网页.写webapp.写hybrid,以及前不久出现的小程序 ...