🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 如果我们在数组中每次成功搜索后加倍,则找到最终值 > 原文: [https://www.geeksforgeeks.org/find-final-value-if-we-double-after-every-successful-search-in-array/](https://www.geeksforgeeks.org/find-final-value-if-we-double-after-every-successful-search-in-array/) 给定一个数组和一个整数 k,遍历该数组,如果数组中的元素为 k,则将 k 的值加倍并继续遍历。 最后返回 k 值。 例子: ``` Input : arr[] = { 2, 3, 4, 10, 8, 1 }, k = 2 Output :16 First k = 2 is found, then we search for 4 which is also found, then we search for 8 which is also found, then we search for 16. Input : arr[] = { 2, 4, 5, 6, 7 }, k = 3 Output :3 ``` 1-如果 arr [i] == k 则遍历数组的每个元素,然后 k = 2 * k 2-重复相同的过程直到数组的最后一个元素 3-最后返回 k 的值 HTG2] ## C++ ```cpp // CPP program to find value if we double  // the value after every successful search #include <bits/stdc++.h> using namespace std; // Function to Find the value of k int findValue(int arr[], int n, int k) {   // Search for k. After every successful   // search, double k.   for (int i = 0; i < n; i++)      if (arr[i] == k)       k *= 2;   return k; } // Driver's Code int main() {   int arr[] = {2, 3, 4, 10, 8, 1}, k = 2;   int n = sizeof(arr) / sizeof(arr[0]);   cout << findValue(arr, n, k);   return 0; } ```