X 我知道了TIPS:左右滑动导航栏可以查看更多栏目
有15个整数按由大到小顺序存放在一个数组中,输入一个整数,要求用二分查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则打印“找不到”。
>> 本文固定链接: http://www.vcgood.com/archives/1652
>> 转载请注明: yuan 2007年06月26日 于 C语言帝国 发表
int find(int a[15], int x){ int low = 0, high = 14, mid; mid = (low+high)/2; while (a[mid] != x) { if (a[mid] < x) { low = mid +1; mid = (low+high)/2; } else { high = mid – 1; mid = (low+high)/2; } } return mid;}
void main(){ int a[15] = {1,5,6,9,12,16,18,26,28,56,59,63,64,69,88}; int x = 59; printf(“the num is: %d “, find(a, x));}
菜鸟报道
你必须先 登录才能发表评论。
int find(int a[15], int x)
{
int low = 0, high = 14, mid;
mid = (low+high)/2;
while (a[mid] != x)
{
if (a[mid] < x)
{
low = mid +1;
mid = (low+high)/2;
}
else
{
high = mid – 1;
mid = (low+high)/2;
}
}
return mid;
}
void main()
{
int a[15] = {1,5,6,9,12,16,18,26,28,56,59,63,64,69,88};
int x = 59;
printf(“the num is: %d “, find(a, x));
}
菜鸟报道