#include <vector>
#include <iostream>
using namespace std;
vector<int> data;
void createData(int n){
//存储小于n的正偶数
for (int i=0;i<n;i+=2)
data.push_back(i);
}
bool search(int value,int& sign){
int start=0;int end=data.size()-1;int mid;
while(start<=end){
mid = (end + start)/2;
if (data.at(mid) == value)
{
sign = mid;
return true;
}else if (data.at(mid) < value)
{
start = mid + 1;
}else{
end = mid -1;
}
}
sign = -1;
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
createData(100);
int sign,ret;
for (int i=-1;i<120;i+=15)
{
ret = search(i,sign);
cout<<"i="<<i<<"; search返回:"<<ret<<"; sign="<<sign<<endl;
}
getchar();
return 0;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。