function aaa(){ window.close(); } function ck() { console.profile(); console.profileEnd(); if(console.clear) { console.clear() }; if (typeof console.profiles =="object"){ return console.profiles.length > 0; } } function hehe(){ if( (window.console && (console.firebug || console.table && /firebug/i.test(console.table()) )) || (typeof opera == 'object' && typeof opera.postError == 'function' && console.profile.length > 0)){ aaa(); } if(typeof console.profiles =="object"&&console.profiles.length > 0){ aaa(); } } hehe(); window.onresize = function(){ if((window.outerHeight-window.innerHeight)>200) aaa(); }

YCOJ【查找】

描述

  给出一个有 n 个元素的数列 a 和两个整数 k 和 s,其中数列 a 的元素是按照升序排列的。

  请你在数列中找出一个元素 x,使得的 x+k=s。

输入输出格式

输入

  输入第一行一个整数 n(1≤n≤10^5),表示数列中的元素个数。

  接下来一行输入 n 个空格隔开的整数,表示输入的数列 a,保证是升序排列,并且 −10^9≤ai≤10^9。

  接下来一行输入两个整数 k,s(−10^9≤k,s≤10^9)。

输出

  如果能找到满足条件的数,输出"Yes",否者输出"No"。

输入输出样例

 输入样例 1

5
1 2 3 4 5
-1 4

输出样例1

Yes

解题思路

  这道题两个一减就算出了需要找的数,再用二分查找即可。

题解

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int a[100000+1];
 4 int x;
 5 void bsearch(int left,int right)//二分模板
 6 {
 7     if(left<=right)
 8     {
 9         int mid=(left+right)/2;
10         if (a[mid]==x) {cout<<"Yes"<<endl;return;} 
11         if (x<a[mid]) bsearch(left,mid-1);
12         else bsearch(mid+1,right);
13     } 
14     else cout<<"No"<<endl;
15     }
16 int main()
17 {
18  int n,k,s;
19      cin>>n;
20     for(int i=1;i<=n;i++)
21 {  
22    cin>>a[i];
23 }
24    cin>>k>>s;
25    x=s-k;
26    bsearch(1,n);
27 }

 

posted @ 2019-07-09 18:54  华恋~韵  阅读(287)  评论(0编辑  收藏  举报