Scanner sc=new Scanner(System.in); System.out.println("输入数组长度"); int n=sc.nextInt(); int a[]=new int [n]; System.out.println("输入数组元素"); for(int i=0;i<n;i++) a[i]=sc.nextInt(); Arrays.sort(a); System.out.println("排序后的数组为"); for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println();
System.out.println("输入要查找的数");
int b=sc.nextInt();
int x=binsearch(a,0,n-1,b);
if(x==-1)
{
System.out.println(b+"不在数组中,请输入另一个数"); b=sc.nextInt();
x=binsearch(a,0,n-1,b);
}
System.out.println(b+"在数组中的第"+(x+1)+"个位置");
}
}
结果:
第二题:棋盘覆盖
搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新教学研究10计本算法实验棋盘覆盖问题(2)全文阅读和word下载服务。
相关推荐: