1.二分查找,java的输入与输出
1 package com; 2 3 4 public class Firstapp{ 5 public static void main(String args[]) 6 { 7 java.util.Scanner sc=new java.util.Scanner(System.in); 8 int n,start,end,middle; 9 System.out.println("请输入:"); 10 n=sc.nextInt(); 11 int a[]={-2,1,4,5,8,12,17,23,45,56,90,100}; 12 start=0; 13 end=a.length-1; 14 middle=(start+end)/2; 15 int count=0; 16 while(n!=a[middle]){ 17 if(n>a[middle]){ 18 start=middle; 19 } 20 else if(n<a[middle]){ 21 end=middle; 22 } 23 middle=(start+end)/2; 24 count++; 25 if(count>a.length/2) 26 break; 27 } 28 if(count>a.length/2) 29 System.out.println(":"+n+"不在数组内"); 30 else 31 System.out.println(":"+n+"是数组中第"+middle+"个元素"); 32 33 } 34 }