import java.util.Scanner; import java.util.Arrays; public class Erfen { public static void main(String[]args) { Scanner sca=new Scanner(System.in); int[] arr={15,22,13,44,5,623,734,18,92,10}; Arrays.sort(arr); int s=0,m=0,e=arr.length-1; System.out.println("请输入一个数在本数组进行查找"); System.out.println(Arrays.toString(arr)); int k=sca.nextInt(); while(true) { m=(s+e)/2; if(arr[m]==k) { System.out.println("在下标"+m+"处"); break; } else if(arr[m]>k) { e=m-1; } } else if(arr[m]<k) { s=m+1; } if(s>e) { System.out.println("查找不到该数字"); break; } } } }
时间: 2024-10-22 13:37:25