<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 重新排列數組,如果`i`為偶數則`arr[i] >= arr[j]`,如果`i`為奇數且`j < i`則 `arr[i] <= arr[j]` > 原文: [https://www.geeksforgeeks.org/rearrange-array-arri-arrj-even-arri/](https://www.geeksforgeeks.org/rearrange-array-arri-arrj-even-arri/) 給定 n 個元素的數組。 我們的任務是編寫一個程序來重新排列數組,以使偶數位置的元素大于其之前的所有元素,奇數位置的元素小于其之前的所有元素。 **示例**: ``` Input : arr[] = {1, 2, 3, 4, 5, 6, 7} Output : 4 5 3 6 2 7 1 Input : arr[] = {1, 2, 1, 4, 5, 6, 8, 8} Output : 4 5 2 6 1 8 1 8 ``` 解決此問題的想法是,首先創建原始數組的輔助副本,然后對復制的數組進行排序。 現在,具有 n 個元素的數組中偶數位置的總數將為`floor(n / 2)`,其余為奇數位置的數量。 現在,以以下方式使用排序后的數組填充原始數組中的奇數和偶數位置: * 總奇數位將為`n – floor(n / 2)`。 從已排序數組的第(`n - floor(n / 2)`)個位置開始,然后將元素復制到已排序數組的第一個位置。 從此位置開始向左遍歷已排序的數組,并繼續向右填充原始數組中的奇數位置。 * 從第(`n - floor(n / 2) + 1`)個位置開始向右遍歷已排序的數組,并從第 2 個位置開始繼續填充原始數組。 以下是上述想法的實現: ## C++ ```cpp // C++ program to rearrange the array // as per the given condition #include <bits/stdc++.h> using namespace std; // function to rearrange the array void rearrangeArr(int arr[], int n) { ????// total even positions ????int evenPos = n / 2; ????// total odd positions ????int oddPos = n - evenPos; ????int tempArr[n]; ????// copy original array in an ????// auxiliary array ????for (int i = 0; i < n; i++) ????????tempArr[i] = arr[i]; ????// sort the auxiliary array ????sort(tempArr, tempArr + n); ????int j = oddPos - 1; ????// fill up odd position in original ????// array ????for (int i = 0; i < n; i += 2) { ????????arr[i] = tempArr[j]; ????????j--; ????} ????j = oddPos; ????// fill up even positions in original ????// array ????for (int i = 1; i < n; i += 2) { ????????arr[i] = tempArr[j]; ????????j++; ????} ????// display array ????for (int i = 0; i < n; i++) ????????cout << arr[i] << " "; } // Driver code int main() { ????int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; ????int size = sizeof(arr) / sizeof(arr[0]); ????rearrangeArr(arr, size); ????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>

                              哎呀哎呀视频在线观看