本文主要是介绍二分查找、三分查找求极点、二分求等比数列【模板】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
二分查找:
int a[110],N;
int BinarySearch(int *a,int x)
{int Left = a[1];int Right = a[N];while(Left <= Right){int mid = (Left+Right)>>1;if(a[mid] == x)return mid;else if(a[mid] > x)Right = mid - 1;elseLeft = mid + 1;}return -1;
}
三分查找求极点:
double left,right,mid,midmid;
left = 0;
right = PI/2;
while(right-left >= 1e-7)
{ mid = (left+right)/2; midmid = (mid+right)/2; if(calc(mid) > calc(midmid)) right = midmid; else left = mid;
}
二分求等比数列
LL Power(LL p,LL n)
{ LL ret = 1; while(n > 0) { if(n&1) ret = ret * p % 9901; p = p * p % 9901; n >>= 1; } return ret;
} LL Sum(LL p,LL n) //递归二分求(1 + p^1 + p^2 + … + p^n)%mod
{ if(n == 0) return 1; if(n&1) return (Sum(p,n/2)*(1+Power(p,n/2+1)))%9901; else return (Sum(p,n/2-1)*(1+Power(p,n/2+1)) + Power(p,n/2))%9901; }
这篇关于二分查找、三分查找求极点、二分求等比数列【模板】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!