Test question algorithm to improve the dichotomy to find array elements (C++)

Resource limit
Time limit: 1.0s Memory limit: 256.0MB
Problem description
  Use recursive function to realize dichotomy to find array elements.
  Supplement: The given array is required to be defined with the following code
  int data[200];
  for (i=0; i<200; i++)
  data[i]=4*i+6;
input format
  input an integer to be searched (the integer Must be in the array data).
Output format
  the indicator of the integer in the array.
Sample input
An input sample that meets the requirements of the question.

Example 1:
262
Example 2:
438
Example 3:
774

Sample output The output
corresponding to the sample input above.
Example 1:
64
Example 2:
108
Example 3:
192

Data scale and convention
  enter the range of each number in the data.
  The input data must satisfy 4*i+6, i=0,1,2,3,...,198,199.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int data[200] ,n;
	for(int i=0;i<200;i++)
		data[i] = 4*i+6;
	cin>>n;
	int l = 0 ,r=199;
	while(l<r){
    
    
		int mid = (l+r)>>1;
		if(data[mid]<n){
    
    
			l = mid+1;
		}else{
    
    
			r = mid;
		}
	}
	cout<<l<<endl;
	return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_51430516/article/details/115218880