弱弱的C语言问题

来源:百度知道 编辑:UC知道 时间:2024/06/14 07:56:53
int binsearch(long a[],int m,long x)
{
int low,high,mid;
low=0;
high=n-1;
while(low<=high)
{
mid=(high+low)/2;
if(x>a[mid]) low=mid+1;
else if(x<a[mid]) high=mid-1;
else return (mid);
}
return(-1);
}
main()
{
int binsearch(long a[],int m,long x);
float score[40];
int n,i,pos;
long num[40],x;
printf("Please enter the total number:");
scanf("%d",&n);
printf ("Please enter the number and score:\n");
for(i=0;i<n;i++)
scanf("%Ld%f",&num[i],&score[i]);
printf("please enter the searching number:\n");
scanf("%Ld",&x);
pos=binsearch(num,n,x);
if(pos!=-1)
printf("score=%4.0\n",score[pos]);
else
printf("not found!\n");
}
应该没打错,要求是二分法查找成绩,把书上的程序copy了一下,但是输入以后总是显示“not found”
不知道为什么。。。TC环境。

这个能编译通过?
high=n-1;
n都没声明。

如果改成m-1的话,我这里可以通过。不过输出的时候
printf("score=%4.0f\n",score[pos]); 这里得加个f。

对了,你的输入方式要是加空格的话,最好在scanf函数里面显示出来
scanf(“%ld %f”,&num[i], &score[i]);

你再看看你是不是哪里写错了(不是程序错,是打错了),因为如果输出是not found,那必然是二分查找函数返回-1,但是我这里可以运行并输出,说明不是算法和语义错误。

出错了。

^^

high=n-1;
--------------
n没有定义