#include
void search(int a[],int k);
void main()
{
int a[15]={15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
search(a,8);
}
void search(int a[],int k)
{
int low,hig,mid;
low=0;hig=14;
mid=(low+hig)/2;
while ((a[mid]!=k)&&low<=hig)
{
if(a[mid]
else
low=mid+1;
mid=(low+hig)/2;
}
if(low>hig)
printf("Not found!\n");
else
printf("the number is the %d\n",mid);
}
#include
void main()
{
int data[15];
int i, j, k;
int n;
for (i = 0; i < 15; i++)
{
scanf("%d", &n);
for (j = 0; j < i; j++)
{
if (n > data[j])
{
for (k = i; k > j; k--)
{
data[k] = data[k - 1];
}
break;
}
}
data[j] = n;
}
scanf ("%d", &n);
int b, e, m;
int index = -1;
for (b = 0, e = 14; e - b > 1; )
{
m = (b + e) / 2;
if (n == data[m])
{
index = m;
break;
} else if (n > data[m])
{
e = m;
} else
{
b = m;
}
}
if (n == data[b])
{
index = b;
} else if (n == data[e])
{
index = e;
}
if (index == -1)
{
printf("无此数");
} else
{
printf("%d", index);
}
}
应该将2楼的输入排序部分和1楼的折半查找部分组合起来,这个程序我认为是最佳的。