提交时间:2023-05-20 17:20:07
运行 ID: 74163
#include<bits/stdc++.h> using namespace std; int search(int arr[], int low, int high, int x){ if(high>=low){ int mid=low+(high-low)/2; if(arr[mid]==x)return mid; else if(arr[mid]>x)return search(arr,low,mid-1,x); else return search(arr,mid+1,high,x); } return -1; } int main(){ int n; cin>>n; int arr[n]; for(int i=0;i<n;i++)cin>>arr[i]; int x; cin>>x; int result=search(arr,0,n-1,x); if(result==-1)cout<<-1<<endl; else cout<<result+1<<endl; return 0; }