标签:std nbsp col color 斐波那契查找 斐波那契 arc rev class
二分查找和斐波那契查找
1 #include<iostream> 2 using namespace std; 3 class Fib{ 4 public:int f,g; 5 Fib(int n) 6 { 7 f=1,g=0; 8 while(g<n) 9 next();}//因构造函数返回函数 10 11 int next(){ 12 g=g+f; 13 f=g-f; 14 return g; 15 } 16 int get(){ 17 return g; 18 } 19 int prev(){ 20 f=g-f; 21 g=g-f; 22 return g; 23 } 24 25 }; 26 int binSearch(int *A,int e,int lo,int hi){ 27 while(lo<hi){ 28 int mi=(lo+hi)>>1; 29 if(e<A[mi])hi=mi; 30 else if(e>A[mi])lo=mi+1; 31 else 32 return mi; 33 } 34 return -1; 35 } 36 int fibSerach(int *A,int e,int lo,int hi){ 37 Fib fib(hi-lo); 38 while(lo<hi){ 39 while(hi-lo<fib.get()) 40 fib.prev(); 41 int mi=lo+fib.get()-1; 42 if(e<A[mi])hi=mi; 43 else if(e>A[mi])lo=mi+1; 44 else 45 return mi; 46 } 47 } 48 49 int main(){ 50 51 52 return 0; 53 }
标签:std nbsp col color 斐波那契查找 斐波那契 arc rev class
原文地址:https://www.cnblogs.com/wsshub/p/12361406.html