|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#define M 10
#include<stdio.h>
void main()
{
static int a[M]={-12,0,6,16,23,56,80,100,110115};//static将数组保存在数据区DATA,没有则保存在堆栈区
int n, low, mid, high, found; //一个程序在编译运行时,普通变量是存放在栈区
low = 0;
high = M-1;
found = 0;
printf("Please input a number:\n");
#if (0) //注释
do
{
scanf("%d",&n);
getchar();
}while(n < a[0]||n > a[M-1]);
#endif
while(("%d",&n)!= 1)
{
printf("Illegal input!!\nPlease input again");
}
/*if(n<a[0]||n>a[M-1]);
{
printf("There is no %d......\n",n);
}*/
while (low <= high)
{
mid = (low + high)/2;
{
found = 1;
break;
}
else if(n > a[mid])
{
low = mid + 1;
}
else
{
high = mid -1;
}
}
if (1 == found)
{
printf("The index of %d is %d\n",n,mid);
}
else
{
printf("There is no %d.....\n",n);
}
}
错误提醒:error else without a previous if
检测不到if
#define M 10
#include<stdio.h>
int main()
{
static int a[M]={-12,0,6,16,23,56,80,100,110115};//static将数组保存在数据区DATA,没有则保存在堆栈区
int n, low, mid, high, found; //一个程序在编译运行时,普通变量是存放在栈区
low = 0;
high = M-1;
found = 0;
printf("Please input a number:\n");
#if (0) //注释
do
{
scanf("%d",&n);
getchar();
}while(n < a[0]||n > a[M-1]);
#endif
while(("%d",&n)!= 1)
{
printf("Illegal input!!\nPlease input again");
}
/*if(n<a[0]||n>a[M-1]);
{
printf("There is no %d......\n",n);
}*/
while (low <= high)
{
mid = (low + high)/2;
if(xxx)
{
found = 1;
break;
}
else if(n > a[mid])
{
low = mid + 1;
}
else
{
high = mid -1;
}
}
if (1 == found)
{
printf("The index of %d is %d\n",n,mid);
}
else
{
printf("There is no %d.....\n",n);
}
return 0;
}
|
|