一、问题描述
输入N个数组成的有序数列,在数组中查找某个数的位置,若找到,输出下标,否则输出not found
二、流程设计
1.取数组最大值最小值下标high和low
2.将待查找数与中值比较,若小于中值则查找前半段,否则查找后半段
3.输出结果
三、流程图设计
四、代码实现
#include <iostream>
using namespace std;
#define N 10
int main() {
int i, a[N] = {-3, 4, 7, 9, 13, 45, 67, 89, 100, 180}, low = 0, high = N - 1, mid, k = -1, m;
for (i = 0; i < N; i++) {
printf("%d", a[i]);
}
cin >> m;
while (low <= high) {
mid = (low + high) / 2;
if (m < a[mid])
high = mid - 1;
else if (m > a[mid])
low = mid + 1;
else {
k = mid;
break;
}
}
if (k >= 0)
printf("m=%d,index=%d\n", m, k);
else
printf("Not be found!\n");
}
标签:int,mid,建民,查找,low,printf,打卡,4.22 From: https://www.cnblogs.com/cor0000/p/17341771.html