PAT 甲级 1048 Find Coins
https://pintia.cn/problem-sets/994805342720868352/problems/994805432256675840
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she could only use exactly two coins to pay the exact amount. Since she has as many as 105 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find two coins to pay for it.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (≤105, the total number of coins) and M (≤103, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers no more than 500. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the two face values V1 and V2 (separated by a space) such that V1+V2=M and V1≤V2. If such a solution is not unique, output the one with the smallest V1. If there is no solution, output No Solution
instead.
Sample Input 1:
8 15
1 2 8 7 2 4 11 15
Sample Output 1:
4 11
Sample Input 2:
7 14
1 8 7 2 4 11 15
Sample Output 2:
No Solution
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int n, target;
int num[maxn]; int main() {
scanf("%d%d", &n, &target);
for(int i = 0; i < n; i ++)
scanf("%d", &num[i]);
sort(num, num + n);
int l = 0, r = n - 1;
int ans1 = -1, ans2 = -1;
while(l < r) {
if(num[l] + num[r] == target) {
ans1 = l;
ans2 = r;
break;
}
if(num[l] + num[r] < target) l ++;
if(num[l] + num[r] > target) r --;
}
if(ans1 == -1 && ans2 == -1)
printf("No Solution\n");
else
printf("%d %d\n", num[ans1], num[ans2]);
return 0;
}
双指针 突然觉得刷 Leetcode 还是很有用的
PAT 甲级 1048 Find Coins的更多相关文章
- PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other ...
- 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 Advanced 1048 Find Coins (25 分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- PAT甲级——A1048 Find Coins
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 ...
- 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甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
随机推荐
- Julia 1.0 中文文档
欢迎来到Julia 1.0的文档. 请阅读发布博客文章,了解该语言的一般概述以及自Julia v0.6以来的许多更改.请注意,0.7版本与1.0一起发布,以提供1.0版本之前的软件包和代码的升级路径. ...
- Django的安装创建与连接数据库
HTTP协议简介 HTTP是一个客户端终端(用户)和服务器端(网站)请求和应答的标准(TCP).通过使用网 页浏览器.网络爬虫或者其它的工具,客户端发起一个HTTP请求到服务器上指定端口(默认端 口为 ...
- lambda方法的引用与构造方法引用
方法的引用 /** * @auther hhh * @date 2018/12/29 22:37 * @description */ public class ObjectMethodUse { /* ...
- Nginx初体验(一):nginx介绍
今天我们来介绍一下Nginx. Nginx是一款轻量级的Web服务器/反向代理服务器以及电子邮件(IMAP/POP3)代理服务器 特点: 反向代理,负载均衡,动静分离 首先我们来介绍一下正向代理服务器 ...
- AS 3.1 多library合并打包成aar的正确方式(fat-aar)
前言 主要参考fat-aar来合并打包. 但是这个fat-aar很久没维护了,如果直接使用它会有很多问题.由于对gradle脚本也不是太熟,就只能顺着它的意思,将gradle降级成2.2.3的版本. ...
- 北京Uber优步司机奖励政策(12月31日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- OpenCV 3.2 Viz 3D可视化
该可视化模块提供了坐标系变化,3D动画等功能 最简单的显示坐标系 viz::Viz3d window("window"); window.showWidget("Coor ...
- 手机蓝牙APP扫描设备的时候异常断开(未完成)
1.手机蓝牙APP打开立马就出现异常,测试在公司有这个问题,在宿舍没这个问题,怀疑是公司设备太多,导致扫描空间不够,或者扫描到奇怪的设备.数组越界之类,明天用log看一下 2. 看样子出了一个erro ...
- you selected does not support x86-64 instruction set
centos 安装redis时报you selected does not support x86-64 instruction set 解决方法 make CFLAGS="-march=x ...
- 一个只有十行的精简MVVM框架(下篇)
本文来自网易云社区. 让我们来加点互动 前面学生信息的身高的单位都是默认m,如果新增一个需求,要求学生的身高的单位可以在m和cm之间切换呢? 首先需要一个变量来保存度量单位,因此这里必须用一个新的Mo ...