1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
#include <string>
using namespace std;
int find(string a[], int low, int high, string target){
int mid = low + (high-low)/2;
if (low > high)
return -1;
if (a[mid] == target)
return mid;
if (a[mid] == ""){
int tmp1 = mid -1 ;
int tmp2 = mid +1 ;
while (a[tmp1] == "" && tmp1>0)
tmp1--;
while (a[tmp2] == "" && tmp2 < 12)
tmp2++;
if (target <= a[tmp1]){
find(a, low, tmp1, target);
}else{
find(a, tmp2, high, target);
}
}
}
void main(){
string s[13] = {"at", "", "", "", "ball", "", "", "car", "", "", "dad", "", ""};
cout<< find(s, 0, 12, "car");
}
|