<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/rearrange-array-such-that-even-positioned-are-greater-than-odd/](https://www.geeksforgeeks.org/rearrange-array-such-that-even-positioned-are-greater-than-odd/) 給定一個 n 個元素的數組`A`,請根據以下關系對數組進行排序: ![ A[i] >= A[i-1] ](https://img.kancloud.cn/d8/ce/d8ced3452cf8e1818dc9d441530ba776_186x28.png "Rendered by QuickLaTeX.com") ,如果`i`是偶數。 ![ A[i] <= A[i-1] ](https://img.kancloud.cn/0e/2f/0e2fd5f457887970549708972dffb722_186x28.png "Rendered by QuickLaTeX.com") ,如果`i`是奇數。 打印結果數組。 例子 : ``` Input : A[] = {1, 2, 2, 1} Output : 1 2 1 2 Explanation : For 1st element, 1 1, i = 2 is even. 3rd element, 1 1, i = 4 is even. Input : A[] = {1, 3, 2} Output : 1 3 2 Explanation : Here, the array is also sorted as per the conditions. 1 1 and 2 < 3. ``` 觀察到該數組由[n / 2]個均勻放置的元素組成。 如果我們將最大[n / 2]個元素分配給偶數位置,而將其余元素分配給奇數位置,那么我們的問題就解決了。 因為處于奇數位置的元素將始終小于處于偶數位置的元素,因為它是最大元素,反之亦然。 對數組進行排序,并在偶數位置分配前[n / 2]個元素。 下面是上述方法的實現: ## C++ ```cpp // C++ program to rearrange the elements? // in array such that even positioned are? // greater than odd positioned elements #include<bits/stdc++.h> using namespace std; void assign(int a[], int n) { ????// Sort the array ????sort(a, a + n); ????int ans[n];? ????int p = 0, q = n - 1; ????for (int i = 0; i < n; i++)? ????{ ????????// Assign even indexes with maximum elements ????????if ((i + 1) % 2 == 0) ????????????ans[i] = a[q--]; ????????// Assign odd indexes with remaining elements ????????else ????????????ans[i] = a[p++]; ????} ????// Print result ????for (int i = 0; i < n; i++)? ????????cout << ans[i] << " "; } // Driver Code int main() { ????int A[] = { 1, 3, 2, 2, 5 }; ????int n = sizeof(A) / sizeof(A[0]); ????assign(A, n); ????return 0; } ```
                  <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>

                              哎呀哎呀视频在线观看