🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 每次成功搜索后通过将元素加倍来重复搜索 > 原文: [https://www.geeksforgeeks.org/repeatedly-search-element-doubling-every-successful-search/](https://www.geeksforgeeks.org/repeatedly-search-element-doubling-every-successful-search/) 给定一个数组“ a []”和整数“ b”。 查找 b 是否存在于 a []中。 如果存在,则将 b 的值加倍并再次搜索。 我们重复这些步骤,直到找不到 b。 最后,我们返回 b 的值。 例子: ``` Input : a[] = {1, 2, 3} b = 1 Output :4 Initially we start with b = 1\. Since it is present in array, it becomes 2. Now 2 is also present in array b becomes 4 . Since 4 is not present, we return 4. Input : a[] = {1 3 5 2 12} b = 3 Output :6 ``` 问题来源:在 Yatra.com 在线测试中提出 1)对输入数组进行排序。 2)继续进行二分搜索并加倍,直到不存在该元素。 以下代码在 STL 中使用 [binary_search()](https://www.geeksforgeeks.org/binary-search-algorithms-the-c-standard-template-library-stl/) ## C++ ```cpp // C++ program to repeatedly search an element by // doubling it after every successful search #include <bits/stdc++.h> using namespace std; int findElement(int a[], int n, int b) {     // Sort the given array so that binary search     // can be applied on it     sort(a, a + n);     int max = a[n - 1]; // Maximum array element     while (b < max) {         // search for the element b present or         // not in array         if (binary_search(a, a + n, b))             b *= 2;         else             return b;     }     return b; } // Driver code int main() {     int a[] = { 1, 2, 3 };     int n = sizeof(a) / sizeof(a[0]);     int b = 1;     cout << findElement(a, n, b);     return 0; } ```