#include"stdafx.h"
#include
using namespace std;
#define NUM 10
int search(int value[], int key, int low, int high);
void main()
{
int value[NUM], result = 1, key = 2;
int i;
cout << "input ten number which are from big to small:" << endl;
for (i = 0; i < NUM; i++)
{
cin >> value[i];/* 输入从大到小的有序数列*/
}
cout << "input one of the ten number:" << endl;
cin >> key; /* 输入待查找的数值*/
result = search(value, key, 0, NUM - 1); /*调用二分查找函数进行查找*/
if (result != -1) cout << "Number." << result << "is :" << key << endl;
else cout << "Can't find the key" << key << endl;
}
int search(int value[], int key, int low, int high)
{
int mid;
if (low > high) return -1; /*查找区间为空*/
else mid = ((low + high) / 2); /*求中间位置*/
if (value[mid] == key) return mid;/*得到查找的数据位置*/
if (key < value[mid]) return search(value, key, low, mid - 1); else return search(value, key, mid + 1, high);/*在下半区查找*/
}
错在哪里?为什么?