📄 6-7-1.cpp
字号:
#include <iostream.h>
#include <stdlib.h>
class BinSearch {
private:
double *Plist;
public:
BinSearch(double *list, int n)
{ Plist = new double[n];
for(int i=0; i < n; i ++) Plist[i] = list[i];
}
~BinSearch()
{ delete []Plist;
}
void BinFind(int, int, double);
};
void BinSearch::BinFind(int low, int high, double x)
{ int mid = (low+high)/2;
if(Plist[mid] == x) {
cout << "the position is: " << mid << endl;
return;
}
else if(low >= high) {
cout << x << " is not found !" << endl;
return;
}
else if(x > Plist[mid])
BinFind(low+1, high, x);
else
BinFind(low, high-1, x);
}
void main()
{ double a[] = {1.1, 1.3, 1.5, 1.7, 1.9,
2.1, 2.3, 2.5, 2.7, 2.9};
double x = 2.4;
int low = 0, high = 9;
BinSearch bs(a, 10);
bs.BinFind(low, high, x);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -