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 ...
随机推荐
- day 18 类与类之间的关系
类与类之间的关系 在我们的世界中事物和事物之间总会有一些联系. 在面向对象中,类和类之间也可以产生相关的关系 1.依赖关系 执行某个动作的时候. 需要xxx来帮助你完成这个操作, ...
- Hive的安装与部署(MySQL作为元数据库)
Hive的安装与部署(MySQL作为元数据) (开始之前确保Hadoop环境已经启动,确保Linux下的MySQL已经安装好) 1. 安装Hive (1)下载安装包 可从apache上下载hi ...
- python代理爬取存入csv文件
爬取高匿代理 from urllib import request import re import time f = open('西1.csv','w',encoding='GBK') header ...
- POJ-3436:ACM Computer Factory (Dinic最大流)
题目链接:http://poj.org/problem?id=3436 解题心得: 题目真的是超级复杂,但解出来就是一个网络流,建图稍显复杂.其实提炼出来就是一个工厂n个加工机器,每个机器有一个效率w ...
- 利用主成分分析(PCA)简化数据
一.PCA基础 线性映射(或线性变换),简单的来说就是将高维空间数据投影到低维空间上,那么在数据分析上,我们是将数据的主成分(包含信息量大的维度)保留下来,忽略掉对数据描述不重要的成分.即将主成分维度 ...
- 使用 Vim 搭建 JavaScript 开发环境
原文链接: https://spacevim.org/cn/use-vim-as-a-javascript-ide/ SpaceVim 是一个模块化的 Vim IDE,针对 JavaScript 这一 ...
- ld: i386 架构于输入文件 bar.o 与 i386:x86-64 输出不兼容
报错:ld: i386 架构于输入文件 foo.o 与 i386:x86-64 输出不兼容 或者:ld: i386 architecture of input file `foo.o' is inco ...
- Luogu2917_ [USACO08NOV]奶牛混合起来Mixed Up Cows_KEY
题目传送门 看到数据范围就果断装压. 设f[i][j]表示i状态下最后一个数字为a[j]. code: #include <cstdio> using namespace std; ]; ...
- Drupal 出错的解决办法
今天安装了superfish菜单模块,安装了一个新菜单后.网站突然打不开了.空白! 第一反应看日志,Apache服务器日志没有发现异常. 可以肯定是添加菜单时,在ATTACH BLOCK部分的区块区域 ...
- 海思NB-IOT的SDK看门狗的使用
1. 看门狗需要喂狗,如果自己写的任务一直运行,那么空闲任务无法运行会导致看门狗复位,来看下看门狗的机制,首先系统启动的时候创建了空闲任务 在这个函数里面void vTaskStartSchedule ...