二分法查找Java算法实现
public static int binary(int a[],int value){
int low=0;
int height=a.length-1;
while(low<=height){
int middle=(height+low)/2;
if(value==a[middle]) return middle;
if(value>a[middle]) return low=middle+1;
if(value<a[middle]) return height=middle-1;
}
return -1;
}
汉罗塔Java算法实现
public class Hanio {
//将n个盘子从x盘借助y盘移动到z盘
public static void hanio(int n ,char x,char y , char z){
if(n==1)
System.out.println(x+"---->"+z);
else
{
hanio(n-1,x,z,y);
System.out.println(x+"---->"+z);
hanio(n-1,y,x,z);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入正整数n和字符x,y,z");
int n=sc.nextInt();
String a= sc.next();
char x=a.charAt(0);
String b=sc.next();
char y=b.charAt(0);
String c=sc.next();
char z=c.charAt(0);
hanio(n,x,y,z);
}
}