<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 旋轉矩陣元素 > 原文: [https://www.geeksforgeeks.org/rotate-matrix-elements/](https://www.geeksforgeeks.org/rotate-matrix-elements/) 給定一個矩陣,順時針旋轉其中的元素。 **示例**: ``` Input 1 2 3 4 5 6 7 8 9 Output: 4 1 2 7 5 3 8 9 6 For 4*4 matrix Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12 ``` 想法是使用類似于[程序的循環以螺旋形式](https://www.geeksforgeeks.org/print-a-given-matrix-in-spiral-form/)打印矩陣。 從最外層開始,一圈一圈地旋轉所有元素環。 要旋轉戒指,我們需要執行以下操作。 1)移動第一行的元素。 2)移動最后一列的元素。 3)移動底部一行的元素。 4)移動第一列的元素。 有內圈時,對內圈重復上述步驟。 以下是上述想法的實現。 感謝 Gaurav Ahirwar 提供以下解決方案的建議。 ## C/C++ ``` // C++ program to rotate a matrix #include <bits/stdc++.h> #define R 4 #define C 4 using namespace std; // A function to rotate a matrix mat[][] of size R x C. // Initially, m = R and n = C void rotatematrix(int m, int n, int mat[R][C]) { ????int row = 0, col = 0; ????int prev, curr; ????/* ???????row - Staring row index ???????m - ending row index ???????col - starting column index ???????n - ending column index ???????i - iterator ????*/ ????while (row < m && col < n) ????{ ????????if (row + 1 == m || col + 1 == n) ????????????break; ????????// Store the first element of next row, this ????????// element will replace first element of current ????????// row ????????prev = mat[row + 1][col]; ?????????/* Move elements of first row from the remaining rows */ ????????for (int i = col; i < n; i++) ????????{ ????????????curr = mat[row][i]; ????????????mat[row][i] = prev; ????????????prev = curr; ????????} ????????row++; ????????/* Move elements of last column from the remaining columns */ ????????for (int i = row; i < m; i++) ????????{ ????????????curr = mat[i][n-1]; ????????????mat[i][n-1] = prev; ????????????prev = curr; ????????} ????????n--; ?????????/* Move elements of last row from the remaining rows */ ????????if (row < m) ????????{ ????????????for (int i = n-1; i >= col; i--) ????????????{ ????????????????curr = mat[m-1][i]; ????????????????mat[m-1][i] = prev; ????????????????prev = curr; ????????????} ????????} ????????m--; ????????/* Move elements of first column from the remaining rows */ ????????if (col < n) ????????{ ????????????for (int i = m-1; i >= row; i--) ????????????{ ????????????????curr = mat[i][col]; ????????????????mat[i][col] = prev; ????????????????prev = curr; ????????????} ????????} ????????col++; ????} ????// Print rotated matrix ????for (int i=0; i<R; i++) ????{ ????????for (int j=0; j<C; j++) ??????????cout << mat[i][j] << " "; ????????cout << endl; ????} } /* Driver program to test above functions */ int main() { ????// Test Case 1 ????int a[R][C] = { {1,? 2,? 3,? 4}, ????????{5,? 6,? 7,? 8}, ????????{9,? 10, 11, 12}, ????????{13, 14, 15, 16}? }; ????// Tese Case 2 ????/* int a[R][C] = {{1, 2, 3}, ??????????????????????{4, 5, 6}, ??????????????????????{7, 8, 9} ?????????????????????}; ?????*/? rotatematrix(R, C, a); ????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>

                              哎呀哎呀视频在线观看