二分查找(输入一个数,输出位数,没有输出不存在)
int a[]={1,5,8,10,16,18,21,26};
Scanner scan = new Scanner(System.in);
int b = scan.nextInt();
int start = 0,end = a.length-1,middle = (start+end)/2;
while(start<=end){
if(b == a[middle]){
System.out.println(middle);
break;
}else if(b < a[middle]){
end = middle-1;
}else{
start = middle+1;
}
middle = (start+end)/2;
}
if(start>end){
System.out.println("不存在");
}
}