首页 练字文章 二分查找(输入一个数,输出位数,没有输出不存在)

二分查找(输入一个数,输出位数,没有输出不存在)

2021-07-25 15:29  浏览数:295  来源:135792468

public static void main(String args[]){
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("不存在");
}
}



声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。

去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)