标签:ann arrays strong while new ring exti common out
1、二分法
int[] arr = {1,2,3,4,5,6,7,8,9,10};
int key = 2;
int min ,max ,mid;
min = 0;
max = arr.length-1;
mid = (min+max)>>1;//(min+max)/2
while (arr[mid]!=key){
if(key>arr[mid]){
min = mid+1 ;
}else if(key<arr[mid]){
max = mid -1;
}
if(max<min){
return;
}
mid = (min+max)>>1;//(min+max)/2
}
System.out.println(mid);
2、冒泡排序
int[] arr1 = {21,14,56,2,5,16,27,83,9,10};
int common ;
for(int i=0;i<arr1.length;i++){
for(int j=i+1;j<arr1.length;j++){
if(arr1[i]<arr1[j]){
common = arr1[i];
arr1[i] = arr1[j];
arr1[j] = common;
}
}
}
System.out.println(Arrays.toString(arr1));
3、求最大公约数、最小公倍数
System.out.println("请输入第一个数字:");
Scanner sc=new Scanner(System.in);
int a = sc.nextInt();
System.out.println("请输入第二个数字:");
int b = sc.nextInt();
int c ;
int mul = 1;
int num = 1 ;
if(a>b){
c=a;
a=b;
b=c;
}
for(int i=2;i<a;i++){
if(a%i==0&&b%i==0){
a=a/i;
b=b/i;
mul*=i;
num = i;
}
}
System.out.println("最小公倍数:"+a*b*mul);
System.out.println("最大公约数:"+num);
标签:ann arrays strong while new ring exti common out
原文地址:http://www.cnblogs.com/zxxshare/p/6369171.html