1048 Find Coins (25 分)

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 10​5​​ 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 (≤10​5​​, the total number of coins) and M (≤10​3​​, 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 V​1​​ and V​2​​ (separated by a space) such that V​1​​+V​2​​=Mand V​1​​≤V​2​​. If such a solution is not unique, output the one with the smallest V​1​​. 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

题目大意:给出n个数,并给出一个两个数的和,判断是否存在这样的两个数,如果有相同的,输出V1较小的结果。

#include <iostream>
#include <algorithm>
#include <vector>
#include<string.h>
#include<string>
#include<cstdio>
using namespace std; int a[];
int main()
{
int n,total;
cin>>n>>total;
for(int i=;i<n;i++){
cin>>a[i];
}
int f=-,s=-;
sort(a,a+n);//从小到大排列。
bool flag=true;
for(int i=;i<n-&&flag;i++){
for(int j=i+;j<n;j++){
if(a[i]+a[j]==total){
f=a[i];
s=a[j];
flag=false;break;
}else if(a[i]+a[j]>total)break;
}
}
if(f!=-&&s!=-)
cout<<f<<" "<<s;
else
cout<<"No Solution";
return ;
}

//一开始写成这个样子,牛客网上通过60%,PAT上有两个测试点没通过,都是因为运行超时,不知如何解决。

下是柳神的解答,实在是叹为观止。

1.使用数组记录数字出现的个数,

2硬币面值不超过500!!!。。

PAT 1048 Find Coins[比较]的更多相关文章

  1. PAT 1048. Find Coins

    two sum题目,算是贪婪吧 #include <cstdio> #include <cstdlib> #include <vector> #include &l ...

  2. PAT 解题报告 1048. Find Coins (25)

    1048. Find Coins (25) Eva loves to collect coins from all over the universe, including some other pl ...

  3. 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 ...

  4. PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)

    1048 Find Coins (25 分)   Eva loves to collect coins from all over the universe, including some other ...

  5. 浙大pat 1048 题解

    1048. Find Coins (25) 时间限制 50 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...

  6. 1048 Find Coins (25 分)

    1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...

  7. PAT 甲级 1048 Find Coins

    https://pintia.cn/problem-sets/994805342720868352/problems/994805432256675840 Eva loves to collect c ...

  8. PAT Advanced 1048 Find Coins (25 分)

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  9. PAT Advanced 1048 Find Coins (25) [Hash散列]

    题目 Eva loves to collect coins from all over the universe, including some other planets like Mars. On ...

随机推荐

  1. Latex之希腊字母表 花体字母 实数集

    花体字母 \mathcal{x} 实数集字母 \mathbb{R} 转自:http://blog.csdn.net/lanchunhui/article/details/49819445 拉丁字母是2 ...

  2. 【BZOJ】3172: [Tjoi2013]单词(后缀自动机)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3172 随便搞个sam就行了.(其实一开始看到数据n<=200, 单词长度不超过1e6,然后感觉 ...

  3. commit命令

    git commit -m "测试提交"

  4. Ubuntu Mysql 安装

    下载 http://dev.mysql.com/downloads/mysql/ 选择 Linux- Generic 选择版本 wget http://cdn.mysql.com/Downloads/ ...

  5. Hibernate_day03--Hibernate多对多操作

    Hibernate多对多操作 多对多映射配置 以用户和角色为例演示 第一步 创建实体类,用户和角色 第二步 让两个实体类之间互相表示 (1)一个用户里面表示所有角色,使用set集合 具体: User. ...

  6. WPF进阶之接口(2):IDisposable,ICollectionView

    废话不多说,进入正题,先来说说IDisposable,看例子(来自MSDN): using System; using System.ComponentModel; // 下面的例子将展示一个实施了I ...

  7. ionic listview对象的编辑、排序和删除

    1)ionic的listview对象即<ion-list></ion-list> 2)添加并显示编辑按钮(添加其他自定义按钮也一样) can-swipe属性设置为true(默认 ...

  8. ios学习--结合UIImageView实现图片的移动和缩放

    因为种种原因,需要在iphone应用中实现图片查看功能,由于iphone屏幕支持多点触摸,于是是想到用“手势”来实现图片的实时缩放和移动.借鉴无所不在的internet网络资料之后,终于实现此一功能, ...

  9. c++11 类型推断

    自动类型推断 当编译器能够在一个变量的声明时候就推断出它的类型,那么你就能够用auto关键字来作为他们的类型: auto x = 1; 编译器当然知道x是integer类型的.所以你就不用int了.接 ...

  10. PHP HTTP协议(报头/状态码/缓存)

    一.HTTP协议介绍 1. #HTTP协议       # (1 建立在TCP/IP协议基础上       # (2 web开发数据传输依赖于http协议       # (3 http 协议全称是文 ...