#include <stdio.h>
typedef struct
{
int r[10000],n,m;
}Node;
void scanf(Node *x){
int i;
for(i=1;i<10000;i++){
scanf("%d",&x->r[i]);
if(getchar()=='\n')
{
break;
}
}
x->n=i;
scanf("%d",&x->m);
}
void bin(Node x,int num) {
int low,high,mid,found,a[10000],i;
low=1;
high=x.n;
found=0;
while((low<=high)&&(found==0))
{
mid=(low+high)/2;
a[num]=mid;
num++;
if(x.m==x.r[mid]) {
found=1;
break;
}
else if(x.m>x.r[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
}
for(i=1;i<num-1;i++){
printf("%d ",a[i]);
}
printf("%d\n",a[num-1]);
if(found==1)
{
printf("%d\n",mid);
}
else
{
printf("%d",found);
}
}
int main(){
Node x;
int num;
while(true){
num=1;
scanf(&x);
bin(x,num);
if (getchar()=='\n')
{
continue;
}
else
{
break;
}
}
return 0;
}
折半查找
最新推荐文章于 2021-05-13 10:36:32 发布