A46400. 和为给定数
填空题
较易
知识点
题目描述
和为给定数
题目描述
给出若干个整数,询问其中是否有一对数的和等于给定的数。
输入
第一行是整数n(0 < n ≤ 100,000),表示有n个整数。
第二行是n个整数。整数的范围是在0到108之间。
第三行是一个整数m(0≤m≤230),表示需要得到的和。
输出
若存在和为m的数对,输出两个整数,小的在前,大的在后,中间用单个空格隔开。若有多个数对满足条件,选择数对中较小的数更小的。若找不到符合要求的数对,输出一行No。
样例输入
4
2 5 1 4
6
样例输出
1 5
参考答案
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
#include <cstdlib>
#define sf(a) scanf("%d\n",&a)
#define pf(a) printf("%.6lf ",a)
#define E 1e-8
#define ms(a) memset(a,0,sizeof a)
#define rep(a,b,c) for(int a=b;a<=c;a++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int idata=1e5+5;
ll minn=inf,maxx=-inf;
ll n,ans,t,k;
char *ch;
ll step[idata];
//ll temp[idata];
ll cnt;
ll query,judge,flag;
priority_queue< int,vector<int>,greater<int> >q;
pair<int,int>p[idata];
int main()
{
while(cin>>n)
{
rep(i,1,n)
cin>>step[i];
stable_sort(step+1,step+1+n);
cin>>query;
rep(i,1,n-1)
{
int temp=query-step[i];
if(binary_search(step+1,step+1+n,temp))
{
cout<<step[i]<<" "<<temp<<endl;
return 0;;
}
}
cout<<"No"<<endl;
}
return 0;
}
上一题
下一题