标签:blog os io for ar 2014 cti div
/********************************************************************
@file Main_practise.cpp
@date 2014-8-15
@author Tiger
@brief 练习
@details 试编写一个递归函数来确定元素x是否属于数组a[0:n-1]。
********************************************************************/
#include <iostream>
const int SIZE = 5;
bool BinSearch(int arr[], int low, int high, int key);
int main(int argc, const char* argv[])
{
int arr[SIZE] = {1, 2, 3, 4, 5};
for (int key=1; key<=6; ++key)
{
if (BinSearch(arr, 0, SIZE-1, key))
{
std::cout << key << " Find." << std::endl;
}
else
{
std::cout << key << " Not Find." << std::endl;
}
}
system("pause");
return 0;
}
bool BinSearch(int arr[], int low, int high, int key)
{
if (low > high)
{
return false;
}
else
{
int mid = static_cast<unsigned int>(low+high) >> 1;
if (arr[mid] == key)
{
return true;
}
else if (arr[mid] > key)
{
return BinSearch(arr, low, mid-1, key);
}
else
{
return BinSearch(arr, mid+1, high, key);
}
}
}
标签:blog os io for ar 2014 cti div
原文地址:http://www.cnblogs.com/roronoa-zoro-zrh/p/3913901.html