跪求大佬看一眼这道c语言改错题 100
程序D1068.c的功能是给定15个有序整数放入数组array中,从键盘上读入一个整数,采用折半查找法在数组array中查找是否存在该整数。
int main(void)
{
int low, high, mid, key;
int array[15] = {1, 3, 6, 8, 12, 14, 23, 45, 67, 68, 78, 79, 100, 112, 223};
printf("\ninput an integer data: ");
scanf("%d", &key);
low = 0, high = 14;
while (low <= high)
{
/*********Found************/
mid = ______________;
if (array[mid] == key)
{
break;
}
else if (array[mid] > key)
{
high = mid - 1;
}
else
{
/*********Found************/
low = ______________;
}
}
/*********Found************/
if (______________)
{
printf("%d is found!\n", key);
}
else
{
printf("%d is not found!\n", key);
}
return 0;
} 展开
#
#include
int main(void)
{
int low, high, mid, key,ok,n;
int array[15] = {1, 3, 6, 8, 12, 14, 23, 45, 67, 68, 78, 79, 100, 112, 223};
printf("*******输出数组******\n");
for(n=0;n<15;n++)
printf("%d ",array[n]);
printf("\ninput an integer data: ");
scanf("%d", &key);
low = 0, high = 14;
while (low <= high)
{
/*********Found************/
mid = low+(high-low)/2;
if (array[mid] == key)
{
ok=1;break;
}
else if (array[mid] > key)
{
high = mid - 1;
}
else
{
/*********Found************/
low = mid+1;
}
ok=-1;
}
/*********Found************/
if (ok==1)
{
printf("%d is found!\n", key);
}
else
{
printf("%d is not found!\n", key);
}
return 0;
}
按照题目丛带要求填写的C语言程序如下(填空的地方见注释)
#include<stdio.h>
int main(void)
{
int low, high, mid, key;
int array[15] = {1, 3, 6, 8, 12, 14, 23, 45, 67, 68, 78, 79, 100, 112, 223};
printf("\ninput an integer data: ");
scanf("%d", 蔽散&key);
low = 0, high = 14;
while (low <= high)
{
/*********Found************/
mid = (low+high)/2;//这里第一处填(low+high)/2
if (array[mid] == key)
{
break;
}
else if (array[mid] > key)
{
high = mid - 1;
}
else
{
/*********Found************/
low = mid+1;//这里第二处填mid+1
}
}
/*********Found************/
if (array[mid] == key)//这里第三处填array[mid] == key
宏郑氏{
printf("%d is found!\n", key);
}
else
{
printf("%d is not found!\n", key);
}
return 0;
}
运行结果
input an integer data: 75
75 is not found!
广告 您可能关注的内容 |