IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> PAT (Advanced Level) Practice 1048 Find Coins (25 分) 凌宸1642 -> 正文阅读

[开发测试]PAT (Advanced Level) Practice 1048 Find Coins (25 分) 凌宸1642

PAT (Advanced Level) Practice 1048 Find Coins (25 分) 凌宸1642

题目描述:

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.

译:Eva 喜欢从宇宙各处收集硬币,包括其他一些行星,如火星。 一天,她参观了一家可以接受各种硬币支付的万能购物中心。 但是,付款有一个特殊要求:每张账单,她只能用恰好两个硬币来支付确切的金额。 因为她身上有多达105个硬币,她肯定需要你的帮助。 你应该告诉她,对于任何给定的金额,她是否能找到两枚硬币来支付。


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.

译:每个输入文件包含一个测试用例。 对于每种情况,第一行包含 2 个正数:N(≤105,硬币总数)和 M(≤103,Eva 必须支付的金额)。 第二行包含N个面值的硬币,均为不超过500的正数。一行中的所有数字之间用空格隔开


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.

译:对于每个测试用例,在一行中打印两个面值 V1 和 V2(以空格分隔),使得 V1+V2=M 且 V1≤V2 。 如果这样的解不唯一,则输出 V1 最小的解。 如果没有解决方案,则输出 No Solution 代替。


Sample Input1 (样例输入1):

8 15
1 2 8 7 2 4 11 15

Sample Output1 (样例输出1):

4 11

Sample Input2 (样例输入2):

7 14
1 8 7 2 4 11 15

Sample Output2 (样例输出2):

No Solution

The Idea:

  • 经典的、简单的双指针问题。
  • 将所有的数字按照升序排序,然后一头一尾指针,指向头尾两个元素,计算两个元素的和:
    • 如果元素的和刚好等于目标值,则直接输出 ,并返回;
    • 如果元素的和小于目标值,则左指针往右移 ;
    • 如果元素的和大于目标值。则右指针往左移 ;
    • 最后如果左指针的位置在右指针右边,则没有找到满足要求的解。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
int n , m ;
int main(){
	cin >> n >> m ;
	vector<int > num(n) ;
	for(int i = 0 ; i < n ; i ++) cin >> num[i] ;
	sort(num.begin() , num.end()) ; // 将输入的元素 升序排列
	int l = 0 , r = n - 1;
	while(l < r){
		if(num[l] + num[r] == m){
			cout << num[l] << ' ' << num[r] << endl ;	
			return 0 ;	
		}else if(num[l] + num[r] < m) l ++ ;
		else r -- ; 
	}
	cout << "No Solution" << endl ;
	return 0 ;
}

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-08-22 13:48:14  更:2021-08-22 13:48:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 8:38:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码