有一整数数组x(正序排列),判断是否有数组元素 x[i]=i 的情况发生#include int index_search(int x[],int n){int first = 0;int last = n-1;int middle,index;index = -1;while (first = 0)printf("\n\nYES,x[%d] = %d has been found.",answer,
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/28 05:28:23
有一整数数组x(正序排列),判断是否有数组元素 x[i]=i 的情况发生#include int index_search(int x[],int n){int first = 0;int last = n-1;int middle,index;index = -1;while (first = 0)printf("\n\nYES,x[%d] = %d has been found.",answer,
有一整数数组x(正序排列),判断是否有数组元素 x[i]=i 的情况发生
#include
int index_search(int x[],int n)
{
int first = 0;
int last = n-1;
int middle,index;
index = -1;
while (first = 0)
printf("\n\nYES,x[%d] = %d has been found.",answer,answer);
else
printf("\n\nNO,there is no element with x[i] = i");
}
3应该填啥
有一整数数组x(正序排列),判断是否有数组元素 x[i]=i 的情况发生#include int index_search(int x[],int n){int first = 0;int last = n-1;int middle,index;index = -1;while (first = 0)printf("\n\nYES,x[%d] = %d has been found.",answer,
answer = index_search(x,n)
/*-------------------------------------------------------
【程序填空】
---------------------------------------------------------
功能:有一整数数组x(正序排列),判断是否有数组元素x[i]=i
的情况发生。
-----------...
全部展开
/*-------------------------------------------------------
【程序填空】
---------------------------------------------------------
功能:有一整数数组x(正序排列),判断是否有数组元素x[i]=i
的情况发生。
-------------------------------------------------------*/
#include
int index_search(int x[], int n)
{
int first = 0;
int last = n-1;
int middle, index;
index = -1;
/***********SPACE***********/
while (first<=last)
{
middle = (first + last) / 2;
/***********SPACE***********/
if (x[middle]==middle)
{
index = middle;
break;
}
/***********SPACE***********/
else if(x[middle]>middle)
last = middle - 1;
else
first = middle + 1;
}
return index;
}
#include
int main()
{
int x[] = { -1, 0, 1, 3, 5, 7, 9, 10};
int n = sizeof(x)/sizeof(int);
int answer, i;
printf("\nindex Search Program");
printf("\n====================");
printf("\n\nGiven Array :");
for (i = 0; i < n; i++)
printf("%5d", x[i]);
/***********SPACE***********/
answer=index_search(x,n);
if (answer >= 0)
printf("\n\nYES, x[%d] = %d has been found.", answer, answer);
else
printf("\n\nNO, there is no element with x[i] = i");
system ("pause");
return 0;
}
收起