``
点击查看代码
import java.util.Scanner;
public class Main {
public static int[] a = new int[10];
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int b = s.nextInt();
for(int i = 0; i < n; i++){
a[i] = s.nextInt();
}
System.out.print( basic(a,b));
}
public static int basic(int[] a, int tar){ //升序数值,找到对应值的下标
int i = 0 , j = a.length - 1;
while(i <= j){
int m = i + j >> 1; //下标取整,3.5 => 3
if(tar > a[m]){ //目标在右边
i = m + 1;
}else if(tar < a[m]){ //目标在左边
j = m -1;
}else{
return m;
}
}
return -1;
}
}