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 ...
随机推荐
- iOS 图片的存储以及读取和删除
将图片存储到本地 NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask ...
- codis__通用的使用模式
1,按功能模块分成不同的productName 参照 sample_user, sample_dynamic (见附件) sample_user.tar.gz,sample_dynamic.tar.g ...
- (转)windows下VMware-workstation中安装CentOS
windows下VMware-workstation中安装CentOS windows下VMware-workstation中安装CentOS,可以分两部分,安装虚拟机和安装CentOS虚拟机.具 ...
- iOS 发送位置消息
发送地理位置在社交应用里面是很常用的需求.最近也需要在自己的应用里面加入这个功能 首先我们需要获取自己的地理位置,这里用到 CLLocationManager 这个类,调用如下代码 locationM ...
- css3里面的-webkit-transition
css3里面的-webkit-transition (1)-webkit-transition-timing-function 可以定义动画的变化时间曲线-webkit-transition-timi ...
- python模块之——tqdm(进度条)
from tqdm import tqdm for i in tqdm(range(10000)): """一些操作""" pass 效果: ...
- 2016级算法第五次上机-B.Bamboo&APTX4844魔发药水
Bamboo&APTX4844魔发药水 题意 "于是,Bamboo耐着性子,看巫师从袖子里掏出 M 瓶时光泉水和 K 粒绿色能量.每瓶时光泉水重量为 c ,生发效果为 l:每粒绿色能 ...
- StringBuffer类和String类的区别
StringBuffer是使用缓冲区的,本身也是操作字符串的,但与String类不同,String类的内容一旦声明后是不可改变的,改变的只是其内存的指向,而StringBuffer类的对象内容是可以改 ...
- 接口自动化之unittest初探
最近几天苦心钻研unittest,终于略有所得,所以想来跟大家分享一下.有关python和unittest的基础知识部分就不在一一细说,相信各位也不是小白了.如果需要我整理基础知识,欢迎留言,我会看情 ...
- rest-assured之Schema validation(包括JSON Schema validation及Xml Schema validation)
rest-assured从2.1.0版本开始支持 Schema 验证,包括JSON Schema validation及Xml Schema validation.我们之前断言响应体都是一个一个字段 ...