package liu.dh.algorithm1;
public class TestBinary {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//1-100之间的数,给定一个数几次查找能够找到这个数;
//查找search=26,看几次能够找到
int first =1;
int laste = 100;
int count = 0;
//Integer
while (true) {
int mid = (first+laste)/2;
int search =26;
if (mid==search) {
count++;
break;
}else if (mid>search) {
laste=mid-1;
count++;
}else if (mid<search) {
first=mid+1;
count++;
}
if (laste<first) {
count=-(count++);
break;
}
if (first>laste) {
count=-(count++);
break;
}
}
System.out.println(count);
}
}
|