<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國際加速解決方案。 廣告
                # 矩陣的鋸齒形(或對角線)遍歷 > 原文: [https://www.geeksforgeeks.org/zigzag-or-diagonal-traversal-of-matrix/](https://www.geeksforgeeks.org/zigzag-or-diagonal-traversal-of-matrix/) 給定 2D 矩陣,以對角線順序打印給定矩陣的所有元素。 例如,考慮以下 5 X 4 輸入矩陣。 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ``` 上述矩陣的對角線打印是 ``` 1 5 2 9 6 3 13 10 7 4 17 14 11 8 18 15 12 19 16 20 ``` 另一個示例: ![diagonal-matrix](https://img.kancloud.cn/02/53/02538ce0f2186b37b26d1c91ad45b9c9_325x347.png) [](https://practice.geeksforgeeks.org/problem-page.php?pid=124) ## 強烈建議您在繼續解決方案之前,單擊此處進行練習。 以下是對角線打印的代碼。 給定矩陣“ matrix [ROW] [COL]”的對角打印輸出中始終帶有“ ROW + COL – 1”行。 ## C++ ```cpp #include <stdio.h> #include <stdlib.h> #define ROW 5 #define COL 4 // A utility function to find min of two integers int minu(int a, int b) { return (a < b)? a: b; } // A utility function to find min of three integers int min(int a, int b, int c) { return minu(minu(a, b), c);} // A utility function to find max of two integers int max(int a, int b) { return (a > b)? a: b; } // The main function that prints given matrix in diagonal order void diagonalOrder(int matrix[][COL]) { ????// There will be ROW+COL-1 lines in the output ????for (int line=1; line<=(ROW + COL -1); line++) ????{ ????????/* Get column index of the first element in this line of output. ???????????The index is 0 for first ROW lines and line - ROW for remaining ???????????lines? */ ????????int start_col =? max(0, line-ROW); ????????/* Get count of elements in this line. The count of elements is ???????????equal to minimum of line number, COL-start_col and ROW */ ?????????int count = min(line, (COL-start_col), ROW); ????????/* Print elements of this line */ ????????for (int j=0; j<count; j++) ????????????printf("%5d ", matrix[minu(ROW, line)-j-1][start_col+j]); ????????/* Ptint elements of next diagonal on next line */ ????????printf("\n"); ????} } // Utility function to print a matrix void printMatrix(int matrix[ROW][COL]) { ????for (int i=0; i< ROW; i++) ????{ ????????for (int j=0; j<COL; j++) ????????????printf("%5d ", matrix[i][j]); ????????printf("\n"); ????} } // Driver program to test above functions int main() { ????int M[ROW][COL] = {{1, 2, 3, 4}, ???????????????????????{5, 6, 7, 8}, ???????????????????????{9, 10, 11, 12}, ???????????????????????{13, 14, 15, 16}, ???????????????????????{17, 18, 19, 20}, ??????????????????????}; ????printf ("Given matrix is \n"); ????printMatrix(M); ????printf ("\nDiagonal printing of matrix is \n"); ????diagonalOrder(M); ????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>

                              哎呀哎呀视频在线观看