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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 數組旋轉的逆向算法 > 原文: [https://www.geeksforgeeks.org/program-for-array-rotation-continued-reversal-algorithm/](https://www.geeksforgeeks.org/program-for-array-rotation-continued-reversal-algorithm/) 編寫一個函數`turn(arr[], d, n)`,該函數將大小為`n`的`arr[]`旋轉`d`個元素。 **示例**: ``` Input : arr[] = [1, 2, 3, 4, 5, 6, 7] d = 2 Output : arr[] = [3, 4, 5, 6, 7, 1, 2] ``` ![Array](https://img.kancloud.cn/fa/6c/fa6cc3eac6c4dd01631156ab85b36e87_395x58.png "Array") 將上面的數組旋轉 2 將使數組 ![ArrayRotation1](https://img.kancloud.cn/8e/2c/8e2cf17600f8a06167071fa55fde816f_395x52.png "ArrayRotation1") 在[這里](https://www.geeksforgeeks.org/array-rotation/)中討論了通過`d`個元素旋轉數組的前 3 種方法。 **方法 4(反向算法)**: **算法**: ``` rotate(arr[], d, n) reverse(arr[], 1, d) ; reverse(arr[], d + 1, n); reverse(arr[], 1, n); ``` 令`AB`為輸入數組的兩個部分,其中`A = arr[0..d-1]`和`B = arr[d..n-1]`。 該算法的思想是: * 反轉`A`以獲得`ArB`,其中`Ar`反轉`A`。 * 反轉`B`以獲得`ArBr`,其中`Br`反轉`B`. * 取反得到`(ArBr)r = BA`。 **示例**: 令數組為 `arr[] = [1, 2, 3, 4, 5, 6, 7]`,`d = 2`和`n = 7` `A = [1, 2]`和`B = [3, 4, 5, 6, 7]` * 反向`A`,我們得到`ArB = [2, 1, 3, 4, 5, 6, 7]` * 反向`B`,我們得到`ArBr = [2, 1, 7, 6, 6, 5, 4, 3]` * 反向所有,我們得到`(ArBr)r = [3, 4, 5, 6, 6, 7, 1, 2]` ` 下面是上述方法的實現: ## C++ ```cpp // C++ program for reversal algorithm // of array rotation #include <bits/stdc++.h> using namespace std; /*Function to reverse arr[] from index start to end*/ void rvereseArray(int arr[], int start, int end) { ????while (start < end) { ????????int temp = arr[start]; ????????arr[start] = arr[end]; ????????arr[end] = temp; ????????start++; ????????end--; ????} } /* Function to left rotate arr[] of size n by d */ void leftRotate(int arr[], int d, int n) { ????if (d == 0) ????????return; ????rvereseArray(arr, 0, d - 1); ????rvereseArray(arr, d, n - 1); ????rvereseArray(arr, 0, n - 1); } // Function to print an array void printArray(int arr[], int size) { ????for (int i = 0; i < size; i++) ????????cout << arr[i] << " "; } /* Driver program to test above functions */ int main() { ????int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????int d = 2; ????// in case the rotating factor is ????// greater than array length ????d = d % n; ????// Function calling ????leftRotate(arr, d, n); ????printArray(arr, 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>

                              哎呀哎呀视频在线观看