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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 正數元素位于偶數位置,負數元素位于奇數位置(不保持相對順序) > 原文: [https://www.geeksforgeeks.org/positive-elements-even-negative-odd-positions/](https://www.geeksforgeeks.org/positive-elements-even-negative-odd-positions/) 給您一個數組,您必須編寫一個程序來轉換該數組,以使正元素出現在數組的偶數位置,而負元素出現在數組的奇數位置。 我們必須做到這一點。 正值和負值的數目可能不相等,多余的值必須保持原樣。 **示例**: ``` Input : arr[] = {1, -3, 5, 6, -3, 6, 7, -4, 9, 10} Output : arr[] = {1, -3, 5, -3, 6, 6, 7, -4, 9, 10} Input : arr[] = {-1, 3, -5, 6, 3, 6, -7, -4, -9, 10} Output : arr[] = {3, -1, 6, -5, 3, -7, 6, -4, 10, -9} ``` 這個想法是使用[快速排序](https://www.geeksforgeeks.org/quick-sort/)的 [Hoare 分區](https://www.geeksforgeeks.org/hoares-vs-lomuto-partition-scheme-quicksort/)處理。 我們采用正面和負面兩個指標。 我們將正指針設置在數組的開頭,將負指針設置在數組的第一個位置。 我們將正指針向前移動兩步,直到找到負元素。 同樣,我們將負指針向前移動兩個位置,直到在其位置找到正值。 如果正負指針位于數組中,則我們將交換這些索引處的值,否則將停止執行該過程。 ## C++ ```cpp // C++ program to rearrange positive and negative // numbers #include <bits/stdc++.h> using namespace std; void rearrange(int a[], int size) { ????int positive = 0, negative = 1; ????while (true) { ????????/* Move forward the positive pointer till? ?????????negative number number not encountered */ ????????while (positive < size && a[positive] >= 0) ????????????positive += 2; ????????/* Move forward the negative pointer till? ?????????positive number number not encountered?? */ ????????while (negative < size && a[negative] <= 0) ????????????negative += 2; ????????// Swap array elements to fix their position. ????????if (positive < size && negative < size) ????????????swap(a[positive], a[negative]); ????????/* Break from the while loop when any index? ????????????exceeds the size of the array */ ????????else ????????????break; ????} } // Driver code int main() { ????int arr[] = { 1, -3, 5, 6, -3, 6, 7, -4, 9, 10 }; ????int n = (sizeof(arr) / sizeof(arr[0])); ????rearrange(arr, n);? ????for (int i = 0; i < n; i++) ????????cout << arr[i] << " "; ????return 0; } ``` ## Java ```java // Java program to rearrange positive // and negative numbers import java.io.*; class GFG { static void rearrange(int a[], int size) { ??int positive = 0, negative = 1, temp; ????while (true) { ????/* Move forward the positive pointer till ????negative number number not encountered */ ????while (positive < size && a[positive] >= 0) ????????positive += 2; ????/* Move forward the negative pointer till ????????positive number number not encountered */ ????while (negative < size && a[negative] <= 0) ????????negative += 2; ????// Swap array elements to fix their position. ????if (positive < size && negative < size) { ????????temp = a[positive]; ????????a[positive] = a[negative]; ????????a[negative] = temp; ????} ????/* Break from the while loop when any index ????exceeds the size of the array */ ????else ????????break; ????} } // Driver code public static void main(String args[]) { ????int arr[] = {1, -3, 5, 6, -3, 6, 7, -4, 9, 10}; ????int n = arr.length; ????rearrange(arr, n); ????for (int i = 0; i < n; i++) ????System.out.print(arr[i] + " "); } } /*This code is contributed by Nikita Tiwari.*/ ``` ## Python3 ```py # Python 3 program to rearrange? # positive and negative numbers def rearrange(a, size) : ????positive = 0 ????negative = 1 ????while (True) : ????????# Move forward the positive ????????# pointer till negative number ????????# number not encountered? ????????while (positive < size and a[positive] >= 0) : ????????????positive = positive + 2 ????????# Move forward the negative? ????????# pointer till positive number ????????# number not encountered? ????????while (negative < size and a[negative] <= 0) : ????????????negative = negative + 2 ????????# Swap array elements to fix ????????# their position. ????????if (positive < size and negative < size) : ????????????temp = a[positive] ????????????a[positive] = a[negative] ????????????a[negative] = temp ????????# Break from the while loop when ????????# any index exceeds the size of? ????????# the array? ????????else : ????????????break # Driver code arr =[ 1, -3, 5, 6, -3, 6, 7, -4, 9, 10 ] n = len(arr) rearrange(arr, n) for i in range(0, n) : ????print(arr[i], end = " ") # This code is contributed by Nikita Tiwari. ``` ## C# ```cs // C# program to rearrange positive // and negative numbers using System; class GFG { // Function to rearrange static void rearrange(int []a, int size) { int positive = 0, negative = 1, temp; ????while (true) { ????// Move forward the positive pointer till ????// negative number number not encountered? ????while (positive < size && a[positive] >= 0) ????????positive += 2; ????// Move forward the negative pointer till ????// positive number number not encountered? ????while (negative < size && a[negative] <= 0) ????????negative += 2; ????// Swap array elements to fix their position. ????if (positive < size && negative < size) { ????????temp = a[positive]; ????????a[positive] = a[negative]; ????????a[negative] = temp; ????} ????// Break from the while loop when any? ????// index exceeds the size of the array? ????else ????????break; ????} } // Driver Code public static void Main(String []args) { ????int []arr = {1, -3, 5, 6, -3, 6, 7, -4, 9, 10}; ????int n = arr.Length; ????rearrange(arr, n); ????for (int i = 0; i < n; i++) ????Console.Write(arr[i] + " "); } } // This code is contributed by Nitin Mittal. ``` ## PHP ```php <?php? // PHP program to rearrange positive? // and negative numbers function rearrange(&$a, $size) { ????$positive = 0; ????$negative = 1; ????while (true)? ????{ ????????/* Move forward the positive? ????????pointer till negative number? ????????number not encountered */ ????????while ($positive < $size &&? ???????????????$a[$positive] >= 0) ????????????$positive += 2; ????????/* Move forward the negative? ????????pointer till positive number ????????number not encountered */ ????????while ($negative < $size &&? ???????????????$a[$negative] <= 0) ????????????$negative += 2; ????????// Swap array elements to fix ????????// their position. ????????if ($positive < $size &&? ????????????$negative < $size) ????????{ ????????????$temp = $a[$positive]; ????????????$a[$positive] = $a[$negative]; ????????????$a[$negative] = $temp; ????????} ????????/* Break from the while loop? ????????when any index exceeds the? ????????size of the array */ ????????else ????????????break; ????} } // Driver code $arr = array( 1, -3, 5, 6, -3,? ??????????????6, 7, -4, 9, 10 ); $n = sizeof($arr); rearrange($arr, $n);? for ($i = 0; $i < $n; $i++) ????echo $arr[$i] ." "; // This code is contributed by ChitraNayal ?> ``` **輸出**: ``` 1 -3 5 -3 6 6 7 -4 9 10 ``` 讓我們在第一個示例上解釋代碼的工作。 `arr[] = {1, -3, 5, 6, -3, 6, 7, -4, 9, 10}` 我們聲明兩個變量為正負,正數指向第零個位置,負數指向第一個位置 正`= 0`,負數`= 1` 在第一次迭代中,由于`a[4]`小于零且正數`= 4`,正數會將 4 個位置移至第五個位置 。 負數將移動 2 個位置,并以`a[3] > 0`指向第四位置。 我們將交換正和負位置值,因為它們小于數組的大小。 第一次迭代后,數組變為`arr[] = {1, -3, 5, -3, 6, 6, 7, -4, 9, 10}` 現在在第四位置的正點和在第三位置的負點 在第二次迭代中,正值將移動 6 位,其值 將比數組的大小大。 負指針將向前移動兩步,并將指向第五個位置。 當正指針值變得大于數組大小時,我們將不執行任何交換操作并退出`while`循環。 最終輸出為 `arr[] = {1, -3, 5, -3, 6, 6, 7, -4, 9, 10}` **不維持相對順序的示例**: `{-1, -2, -3, -4, -5, 6, 7, 8};` **另一種方法:-** 的想法是找到一個不正確位置的正/負元素(即,在奇數位置為正,在偶數位置為負),然后找到符號相反的元素,即 在剩余數組中的錯誤位置,然后交換這兩個元素。 這是上述想法的實現。 ## C++ ``` // C++ program to rearrange positive // and negative numbers #include<iostream> using namespace std; // Swap function void swap(int* a, int i , int j) { ????int temp = a[i]; ????a[i] = a[j]; ????a[j] = temp; ????return ; } // Print array function void printArray(int* a, int n) { ????for(int i = 0; i < n; i++) ????????cout << a[i] << " "; ????cout << endl; ????return ; } // Driver code int main() { ????int arr[] = { 1, -3, 5, 6, -3, 6, 7, -4, 9, 10 }; ????int n = sizeof(arr)/sizeof(arr[0]); ????//before modification ????printArray(arr, n); ????for(int i = 0; i < n; i++) ????{ ????????if(arr[i] >= 0 && i % 2 == 1) ????????{? ????????????// out of order positive element ????????????for(int j = i + 1; j < n; j++) ????????????{ ????????????????if(arr[j] < 0 && j % 2 == 0) ????????????????{? ????????????????????// find out of order negative? ????????????????????// element in remaining array ????????????????????swap(arr, i, j); ????????????????????break ; ????????????????} ????????????} ????????} ????????else if(arr[i] < 0 && i % 2 == 0) ????????{ ????????????// out of order negative element ????????????for(int j = i + 1; j < n; j++) ????????????{ ????????????????if(arr[j] >= 0 && j % 2 == 1) ????????????????{ ????????????????????// find out of order positive? ????????????????????// element in remaining array ????????????????????swap(arr, i, j); ????????????????????break; ????????????????} ????????????}? ????????} ????} ????//after modification ????printArray(arr, n);? ????return 0; } // This code is contributed by AnitAggarwal ```
                  <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>

                              哎呀哎呀视频在线观看