base查找方法的实现JAVA

import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
/*在一个有序数组中查找一个数的过程,模拟二分法查找,如果在左边区域就输出0,如果在右边区域就输出1,最后结果输出整个过程的1、0,精确度是6*/
public class base {

    public static void main(String args[]){
        Scanner cin = new Scanner(System.in);
        int x = cin.nextInt();
        int[] y = new int[181];
        for(int i=0;i<181;i++){
            y[i]=i-90;
        }
        List<Integer> z = search(x,y);
        for(int i:z){
            System.out.print(i);
        }
            
    }
    public static List<Integer> search(int x,int[] R){
        int length = R.length;
        int mid,low=0,high=length-1;
        ArrayList<Integer> temp = new ArrayList<Integer>();
        while((high-low)>6){
            mid=(high+low)/2;
            if(x==R[mid]){
                break;
            }
            if(x<R[mid]){
                high=mid-1;
                temp.add(0);        
            }else if(x>R[mid]){
                low=mid+1;
                temp.add(1);
            }
        }
        return temp;
    }
    
    
}
原文地址:https://www.cnblogs.com/Janejxt/p/5864652.html