<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 天花板排列 > 原文: [https://www.geeksforgeeks.org/ceiling-in-a-sorted-array/](https://www.geeksforgeeks.org/ceiling-in-a-sorted-array/) 給定一個排序數組和一個值 x,x 的上限是數組中大于或等于 x 的最小元素,下限是小于或等于 x 的最大元素。 假設數組以非降序排序。 編寫有效的函數以查找 x 的底和頂。 **示例**: ``` For example, let the input array be {1, 2, 8, 10, 10, 12, 19} For x = 0: floor doesn't exist in array, ceil = 1 For x = 1: floor = 1, ceil = 1 For x = 5: floor = 2, ceil = 8 For x = 20: floor = 19, ceil doesn't exist in array ``` 在以下方法中,我們僅實現了上限搜索功能。 樓層搜索可以以相同的方式實現。 **方法 1(線性搜索)** 用于搜索 x 上限的算法: 1)如果 x 小于或等于數組中的第一個元素,則返回 0(第一個元素的索引)[ 2)其他線性搜索索引 i,以使 x 介于 arr [i]和 arr [i + 1]之間。 3)如果在步驟 2 中找不到索引 i,則返回-1 ## C++ ```cpp // C++ implementation of above approach #include <bits/stdc++.h> using namespace std; /* Function to get index of ceiling of x in arr[low..high] */ int ceilSearch(int arr[], int low, int high, int x)? {? ????int i;? ????/* If x is smaller than or equal to first element,? ????????then return the first element */ ????if(x <= arr[low])? ????????return low;? ????/* Otherwise, linearly search for ceil value */ ????for(i = low; i < high; i++)? ????{? ????????if(arr[i] == x)? ????????return i;? ????????/* if x lies between arr[i] and arr[i+1] including? ????????arr[i+1], then return arr[i+1] */ ????????if(arr[i] < x && arr[i+1] >= x)? ????????return i+1;? ????}????? ????/* If we reach here then x is greater than the last element? ????????of the array, return -1 in this case */ ????return -1;? }? /* Driver code*/ int main()? {? ????int arr[] = {1, 2, 8, 10, 10, 12, 19};? ????int n = sizeof(arr)/sizeof(arr[0]);? ????int x = 3;? ????int index = ceilSearch(arr, 0, n-1, x);? ????if(index == -1)? ????????cout << "Ceiling of " << x << " doesn't exist in array ";? ????else ????????cout << "ceiling of " << x << " is " << arr[index];? ????return 0;? }? // This is code is contributed by rathbhupendra ```
                  <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>

                              哎呀哎呀视频在线观看