[求助]程序输不出结果
public class test
{
public static void main(String[] args){
int a[]={12,34,11,3,23,41,12,1};
for(int i=0;i<7;i++)
{
for(int j=0;j<7-i;j++)
{
if(a[j]>a[j+1])
{
int t;
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(int i=0;i<8;i++)
System.out.print(a[i]+" ");
System.out.println();
find b=new find();
System.out.println(b.finded(a,23,8));
}
}
class find
{
public int finded(int a[],int value,int size)
{
int i=0;
int start=0;
int end=size-1;
int mid=(start+end)/2;
while(start<=size-1)
{
if(a[mid]==value)
{
i=mid;
break;
}
if(a[mid]>value)
start=mid+1;
else
end=mid-1;
}
return i;
}
}
程序序不出我要找的元素
搜索更多相关的解决方案:
结果
----------------解决方案--------------------------------------------------------
public class test
{
public static void main(String[] args){
int a[]={12,34,11,3,23,41,12,1};
for(int i=0;i<7;i++)
{
for(int j=0;j<7-i;j++)
{
if(a[j]>a[j+1])
{
int t;
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(int i=0;i<8;i++)
System.out.print(a[i]+" ");
System.out.println();
find b=new find();
System.out.println(b.finded(a,23,8));
}
}
class find
{
public int finded(int a[],int value,int size)
{
int i=0;
int start=0;
int end=size-1;
int mid;
while(start<=size-1)
{
mid = (start+end)/2;//你位置错了
if(a[mid]==value)
{
i=mid;
break;
}
if(a[mid]<value)//你弄反了
start=mid+1;
else
end=mid-1;
}
return i;
}
}
----------------解决方案--------------------------------------------------------
return i+1 吧!
----------------解决方案--------------------------------------------------------
位置应该没错吧,弄反了倒是真的,照各位的做法做了,还是那样,真是郁闷,我自己研究一下吧,谢谢各位啦
----------------解决方案--------------------------------------------------------
呵呵,谢啦,是我自己的错,竟把mid(start+end)/2放在while循环里面
----------------解决方案--------------------------------------------------------
mid=(start+end)/2 必须写在for循环里面啊
----------------解决方案--------------------------------------------------------
呵呵,知道啊
----------------解决方案--------------------------------------------------------