#P1759. 二分查找(递归).填空题

二分查找(递归).填空题

题目描述

请在一个有序递增数组中(不存在相同元素),采用二分查找,找出值 x的位置,如果 x 在数组中不存在,请输出 -1 。

输入格式

第一行,一个整数 n,代表数组元素个数 ( n <= 106{10}^6 )

第二行,n 个数,代表数组的 n 个递增元素 ( 1 <= 数组元素值 <= 108{10}^8 )

第三行,一个整数 x ,代表要查找的数( 0 <= x <= 108{10}^8

输出格式

x 在数组中的位置,或者 -1 。

样例

10
1 3 5 7 9 11 13 15 17 19
3
2

程序填空

#include<bits/stdc++.h>
using namespace std;
int a[1000001],n,x;
int Find(int low,int high)
{
	
	if(填空(1)>x||填空(2)<x||填空(3) ) // 不可能找得到的3种情况
		return -1;
	
	int mid=(low+high)/2;
	if(a[mid]==x)
		return 填空(4);
	else if(a[mid]<x)
		return Find(填空(5),填空(6));
	else
		return Find(填空(7),填空(8));
}
int main()
{
	int i;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	scanf("%d",&x);

	printf("%d",Find(填空(9),填空(10)));	

	return 0;
}

填空(1):{{ input(1) }}

填空(2):{{ input(2) }}

填空(3):{{ input(3) }}

填空(4):{{ input(4) }}

填空(5):{{ input(5) }}

填空(6):{{ input(6) }}

填空(7):{{ input(7) }}

填空(8):{{ input(8) }}

填空(9):{{ input(9) }}

填空(10):{{ input(10) }}