<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 查找數組中最接近的數字 > 原文: [https://www.geeksforgeeks.org/find-closest-number-array/](https://www.geeksforgeeks.org/find-closest-number-array/) 給定一個排序的整數數組。 我們需要找到最接近給定數字的值。 數組可能包含重復值和負數。 **示例**: ``` Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input :arr[] = {2, 5, 6, 7, 8, 8, 9}; Target number = 4 Output : 5 ``` **簡單解決方案**是遍歷給定數組,并跟蹤每個元素與當前元素的絕對差。 最后返回具有最小絕對值差的元素。 一種有效的**解決方案**是使用[二分搜索](https://www.geeksforgeeks.org/binary-search/)。 ## C++ ```cpp // CPP program to find element // closet to given target. #include <bits/stdc++.h> using namespace std; int getClosest(int, int, int); // Returns element closest to target in arr[] int findClosest(int arr[], int n, int target) { ????// Corner cases ????if (target <= arr[0]) ????????return arr[0]; ????if (target >= arr[n - 1]) ????????return arr[n - 1]; ????// Doing binary search ????int i = 0, j = n, mid = 0; ????while (i < j) { ????????mid = (i + j) / 2; ????????if (arr[mid] == target) ????????????return arr[mid]; ????????/* If target is less than array element, ????????????then search in left */ ????????if (target < arr[mid]) { ????????????// If target is greater than previous ????????????// to mid, return closest of two ????????????if (mid > 0 && target > arr[mid - 1]) ????????????????return getClosest(arr[mid - 1], ??????????????????????????????????arr[mid], target); ????????????/* Repeat for left half */ ????????????j = mid; ????????} ????????// If target is greater than mid ????????else { ????????????if (mid < n - 1 && target < arr[mid + 1]) ????????????????return getClosest(arr[mid], ??????????????????????????????????arr[mid + 1], target); ????????????// update i ????????????i = mid + 1;? ????????} ????} ????// Only single element left after search ????return arr[mid]; } // Method to compare which one is the more close. // We find the closest by taking the difference // between the target and both values. It assumes // that val2 is greater than val1 and target lies // between these two. int getClosest(int val1, int val2, ???????????????int target) { ????if (target - val1 >= val2 - target) ????????return val2; ????else ????????return val1; } // Driver code int main() { ????int arr[] = { 1, 2, 4, 5, 6, 6, 8, 9 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????int target = 11; ????cout << (findClosest(arr, n, target)); } // This code is contributed bu Smitha Dinesh Semwal ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看