public class Search{
public static void main(String [] args) {
int a[] = {1, 3, 4, 7, 9, 20, 58, 79 };
int i = 9;
System.out.println(twofen(a,i));
}
}
public static int twofen(int []a,int num) {
if(a.length == 0)return -1;
int firstpos = 0;
int lastpos = a.length -1;
int m = (firstpos + lastpos) / 2;
while(firstpos <= lastpos){
if(num == a[m])return m;
if(num < a[m]){lastpos = a[m]-1;}
if(num > a[m]){firstpos = a[m]+1;}
m = (firstpos + lastpos) / 2;
}
return -1;
}
public static void main(String [] args) {
int a[] = {1, 3, 4, 7, 9, 20, 58, 79 };
int i = 9;
System.out.println(twofen(a,i));
}
}
public static int twofen(int []a,int num) {
if(a.length == 0)return -1;
int firstpos = 0;
int lastpos = a.length -1;
int m = (firstpos + lastpos) / 2;
while(firstpos <= lastpos){
if(num == a[m])return m;
if(num < a[m]){lastpos = a[m]-1;}
if(num > a[m]){firstpos = a[m]+1;}
m = (firstpos + lastpos) / 2;
}
return -1;
}
解决方案 »
- 求java聊天软件,来信息时头像闪动的原理
- java String转java.sql.Date
- java调用webservices问题
- 怎么将ACCESS中的OLE图片对象导出一个图片文件?
- 如何在frame上画一个矩形,并控制对它的匀速直线运动和停止?
- java.util.Timer类的使用问题。
- 请教:如何将一个固定大小的String转换为整数二维数组(对,不是一维)
- 国内有什么讲解Jbiulder的好书?请告知,谢谢!!
- 关于protected跨包访问的疑问---定义的正确解释?
- 那种JAVA最好?
- 流操作java.io.EOFException求解释
- 关于static问题的一个程序求解释
public static void main(String [] args) {
int a[] = {1, 3, 4, 7, 9, 20, 58, 79 };
int i = 9;
System.out.println(twofen(a,i));
} public static int twofen(int []a,int num) {
if(a.length == 0)return -1;
int firstpos = 0;
int lastpos = a.length -1;
int m = (firstpos + lastpos) / 2;while(firstpos <= lastpos){
if(num == a[m])return m;
if(num < a[m]){lastpos = (m-1)/2;}
if(num > a[m]){firstpos = (m+1)/2;}
m = (firstpos + lastpos) / 2;
}
return -1;
}
}输出结果4,a[4]为9.
while(firstpos <= lastpos){
if(num == a[m])return m;
if(num < a[m]){lastpos = (m-1)/2;}
if(num > a[m]){firstpos = (m+1)/2;}
m = (firstpos + lastpos) / 2;
return -1;
}
修改成
while(firstpos <= lastpos){
if(num == a[m])return m;
if(num < a[m]){lastpos = (m-1)/2;}
if(num > a[m]){firstpos = (m+1)/2;}
m = (firstpos + lastpos) / 2;
return m;}
}
return -1;
}
public static void main(String [] args) {
int a[] = {1, 3, 4, 7, 9, 20, 58, 79 };
int i = -1;
System.out.println(twofen(a,i));
} public static int twofen(int []a,int num) {
if(a.length == 0)return -1;
int firstpos = 0;
int lastpos = a.length -1;
while(firstpos <=lastpos){
if(num == a[(firstpos + lastpos) / 2])return (firstpos + lastpos) / 2;
if(num < a[(firstpos + lastpos) / 2]){lastpos = ((firstpos + lastpos) / 2)-1;}
if(num > a[(firstpos + lastpos) / 2]){firstpos = ((firstpos + lastpos) / 2)+1;}}
return -1;
}
}
lastpos = a[m]-1;
}
if(num > a[m]){
firstpos = a[m]+1;
}两名改为:
if(num < a[m]){
lastpos = m-1;
}
if(num > a[m]){
firstpos = m+1;
}
2,“if(num < a[m]){lastpos = a[m]-1;}”须将 a[m]-1改为m-1
及“if(num > a[m]){firstpos = a[m]+1;}“里的a[m]+1改为m+1即可
public static void main(String [] args) {
int a[] = {1, 3, 4, 7, 9, 20, 58, 79 };
int i = 9;
System.out.println(twofen(a,i));
}
public static int twofen(int []a,int num) {
if(a.length == 0) return -1;
int firstpos = 0;
int lastpos = a.length -1;
int m = (firstpos + lastpos) / 2;
while(firstpos <= lastpos){
if(num == a[m]) return m;
if(num < a[m]){ lastpos = m-1;}
if(num > a[m]){ firstpos = m+1;}
m = (firstpos + lastpos) / 2;
}
return -1;
}
}
1 函数应放Search类里
2 if(num < a[m]){lastpos = a[m]-1;}
if(num > a[m]){firstpos = a[m]+1;} 错了 应该对其排列号进行操作 而不是更高数的大小