标签:二分查找 mes pac test 文件 code col 函数 fun
模板来自 www.acwing.com
// 23132423543426561213.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <vector> using namespace std; int test1[] = {1,3,5,5,5,5,5,5,5,11,12,16,99,100}; int test2[] = { 1,1,2,2,3,3,3,3,4,7,8,8,9,9,9,9,11,12,14,18,99,100 }; void fun1(int arr[],int len ) { int l = 0; int r = len - 1; while (l < r) { int mid = l + r >> 1; if (arr[mid] >= 5) r = mid; else l = mid + 1; } cout << "l = " << l << ".r = " << r << ". arr[l] = " << arr[l] << ". arr[r] = " << arr[r] << endl; l = 0; r = len - 1;; while (l < r) { int mid = (l + r + 1) >> 1; if (arr[mid] <= 5) l = mid; else r = mid - 1; } cout << "l = " << l << ".r = " << r << ". arr[l] = " << arr[l] << ". arr[r] = " << arr[r] << endl; } int main() { //fun1(test1,sizeof(test1)/sizeof(test1[0])); fun1(test2, sizeof(test2) / sizeof(test2[0])); return 0; }
二分查找的 上边界和下边界 以及最接近上边界和最接近下边界的例子
标签:二分查找 mes pac test 文件 code col 函数 fun
原文地址:https://www.cnblogs.com/itdef/p/13162048.html