<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國際加速解決方案。 廣告
                # 向右旋轉矩陣 K 次 > 原文: [https://www.geeksforgeeks.org/rotate-matrix-right-k-times/](https://www.geeksforgeeks.org/rotate-matrix-right-k-times/) 給定一個大小為 N * M 且值為 K 的矩陣。我們必須將矩陣向右旋轉 K 次。 **示例**: ``` Input : N = 3, M = 3, K = 2 12 23 34 45 56 67 78 89 91 Output : 23 34 12 56 67 45 89 91 78 Input : N = 2, M = 2, K = 2 1 2 3 4 Output : 1 2 3 4 ``` 一種簡單而有效的方法是將矩陣的每一行視為一個數組并執行數組旋轉。 可以通過使用臨時數組將元素從 K 復制到數組的末尾到數組的開頭來完成。 然后剩下的元素從開始到 K-1 到結束。 讓我們舉個例子: ![](https://img.kancloud.cn/fe/a8/fea861b9803d491f5c5304a4f19139e6_536x192.png) ## C++ ```cpp // CPP program to rotate a matrix right by k times #include <iostream> // size of matrix #define M 3 #define N 3 using namespace std; // function to rotate matrix by k times void rotateMatrix(int matrix[][M], int k) { ??// temporary array of size M ??int temp[M]; ??// within the size of matrix ??k = k % M; ??for (int i = 0; i < N; i++) { ????// copy first M-k elements to temporary array ????for (int t = 0; t < M - k; t++) ??????temp[t] = matrix[i][t]; ????// copy the elements from k to end to starting ????for (int j = M - k; j < M; j++) ??????matrix[i][j - M + k] = matrix[i][j]; ????// copy elements from temporary array to end ????for (int j = k; j < M; j++) ??????matrix[i][j] = temp[j - k]; ??} } // function to display the matrix void displayMatrix(int matrix[][M]) { ??for (int i = 0; i < N; i++) { ????for (int j = 0; j < M; j++) ??????cout << matrix[i][j] << " "; ????cout << endl; ??} } // Driver's code int main() { ??int matrix[N][M] = {{12, 23, 34}, ?????????????????????{45, 56, 67},? ?????????????????????{78, 89, 91}}; ??int k = 2; ??// rotate matrix by k ??rotateMatrix(matrix, k); ??// display rotated matrix ??displayMatrix(matrix); ??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>

                              哎呀哎呀视频在线观看