二分法求输入数在数组中的序号
#include <stdio.h>#define M 10
void main()
{
static int a={-12,0,6,16,23,56,80,100,110,115};
int n,low,mid,high,found;
low=0;
high=M-1;
found=0;
scanf("%d,&n");
while(low<=high)
{
mid=(low+high)/2;
if(a==n)
{
found=1;
break;
}
else if(a>n)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
if(found==1)
{
printf("is %d",mid);
}
else
{
printf("is not");
}
}
为什么无论输入什么得到的都是1? #include <stdio.h>
#define M 10
int main()
{
static int a={-12,0,6,16,23,56,80,100,110,115};
int n,low,mid = 0,high,found;
low=0;
high=M-1;
found=0;
scanf("%d",&n);//这里你写错了
while(low<=high)
{
mid=(low+high)/2;
if(a==n)
{
found=1;
break;
}
else if(a>n)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
if(found==1)
{
printf("is %d",mid+1);//这里加一表示第几个数字
}
else
{
printf("is not");
}
return 0;
} #include <stdio.h>
#define M 10
int main()
{
static int a={-12,0,6,16,23,56,80,100,110,115};
int n,low,mid = 0,high,found;
low=0;
high=M-1;
found=0;
scanf("%d",&n);//这里你写错了
while(low<=high)
{
mid=(low+high)/2;
if(a==n)
{
found=1;
break;
}
else if(a>n)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
if(found==1)
{
printf("is %d",mid);//这里加一表示数组序列
}
else
{
printf("is not");
}
return 0;
}
这样写表示数组中的序列 学习学习 超凡天赐 发表于 2017-3-6 21:52
这样写表示数组中的序列
原来是双引号放错位置了。谢谢啦!
页:
[1]