<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/print-matrix-snake-pattern/](https://www.geeksforgeeks.org/print-matrix-snake-pattern/) 給定一個 n x n 矩陣。在給定的矩陣中,您必須以蛇形圖案打印矩陣的元素。 **示例**: ``` Input :mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}}; Output : 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input :mat[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output : 1 2 3 6 5 4 7 8 9 ``` ![Matrix printing](https://img.kancloud.cn/10/b7/10b7036fe6af892285a502598902b929_401x353.png) 我們遍歷所有行。 對于每一行,我們檢查它是偶數還是奇數。 如果是偶數,我們從左到右打印,否則從右到左打印。 ## C++ ```cpp // C++ program to print matrix in snake order #include <iostream> #define M 4 #define N 4 using namespace std; void print(int mat[M][N]) { ????// Traverse through all rows ????for (int i = 0; i < M; i++) { ????????// If current row is even, print from ????????// left to right ????????if (i % 2 == 0) { ????????????for (int j = 0; j < N; j++) ????????????????cout << mat[i][j] << " "; ????????// If current row is odd, print from ????????// right to left ????????} else { ????????????for (int j = N - 1; j >= 0; j--) ????????????????cout << mat[i][j] << " "; ????????} ????} } // Driver code int main() { ????int mat[][] = { { 10, 20, 30, 40 }, ????????????????????{ 15, 25, 35, 45 }, ????????????????????{ 27, 29, 37, 48 }, ????????????????????{ 32, 33, 39, 50 } }; ????print(mat); ????return 0; } ``` ## Java ```java // Java program to print matrix in snake order import java.util.*; class GFG { ????static void print(int [][] mat) ????{ ????????// Traverse through all rows ????????for (int i = 0; i < mat.length; i++) ????????{ ????????????// If current row is even, print from ????????????// left to right ????????????if (i % 2 == 0) ????????????{ ????????????????for (int j = 0; j < mat[0].length; j++) ????????????????????System.out.print(mat[i][j] +" "); ????????????// If current row is odd, print from ????????????// right to left ????????????} ????????????else ????????????{ ????????????????for (int j = mat[0].length - 1; j >= 0; j--) ????????????????????System.out.print(mat[i][j] +" "); ????????????} ????????} ????} ????// Driver code ????public static void main(String[] args) ????{ ????????int mat[][] = new int[][] ????????{ ????????????{ 10, 20, 30, 40 }, ????????????{ 15, 25, 35, 45 }, ????????????{ 27, 29, 37, 48 }, ????????????{ 32, 33, 39, 50 } ????????}; ????????print(mat); ????} } /* This code is contributed by Mr. Somesh Awasthi */ ``` ## Python 3 ``` # Python 3 program to print # matrix in snake order M = 4 N = 4 def printf(mat): ????global M, N ????# Traverse through all rows ????for i in range(M): ????????# If current row is ????????# even, print from ????????# left to right ????????if i % 2 == 0: ????????????for j in range(N): ????????????????print(str(mat[i][j]), ??????????????????????????end = " ") ????????# If current row is? ????????# odd, print from ????????# right to left ????????else: ????????????for j in range(N - 1, -1, -1): ????????????????print(str(mat[i][j]),? ??????????????????????????end = " ") # Driver code mat = [[ 10, 20, 30, 40 ], ???????[ 15, 25, 35, 45 ], ???????[ 27, 29, 37, 48 ], ???????[ 32, 33, 39, 50 ]] printf(mat) # This code is contributed # by ChitraNayal ``` ## C# ```cs // C# program to print? // matrix in snake order using System; class GFG { ????static void print(int [,]mat) ????{ ????????// Traverse through all rows ????????for (int i = 0;? ?????????????????i < mat.GetLength(0); i++) ????????{ ????????????// If current row is? ????????????// even, print from? ????????????// left to right ????????????if (i % 2 == 0) ????????????{ ????????????????for (int j = 0;? ?????????????????????????j < mat.GetLength(1); j++) ????????????????????Console.Write(mat[i, j] + " "); ????????????// If current row is? ????????????// odd, print from? ????????????// right to left ????????????} ????????????else ????????????{ ????????????????for (int j = mat.GetLength(1) - 1;? ?????????????????????????j >= 0; j--) ????????????????????Console.Write(mat[i, j] + " "); ????????????} ????????} ????} ????// Driver code ????public static void Main() ????{ ????????int [,]mat = {{ 10, 20, 30, 40 }, ??????????????????????{ 15, 25, 35, 45 }, ??????????????????????{ 27, 29, 37, 48 }, ??????????????????????{ 32, 33, 39, 50 }}; ????????print(mat); ????} } // This code is contributed // by ChitraNayal ``` ## PHP ```php <?php // PHP program to print? // matrix in snake order $M= 4; $N =4; function printLN($mat) { ????global $M; ????global $N; ????// Traverse through all rows ????for ($i = 0; $i < $M; $i++)? ????{ ????????// If current row is even,? ????????// print from left to right ????????if ($i % 2 == 0)? ????????{ ????????????for ($j = 0; $j < $N; $j++) ????????????????echo $mat[$i][$j], " "; ????????// If current row is odd,? ????????// print from right to left ????????}? ????????else? ????????{ ????????????for ($j = $N - 1; $j >= 0; $j--) ????????????????echo $mat[$i][$j] , " "; ????????} ????} } // Driver code $mat = array(array(10, 20, 30, 40), ?????????????array(15, 25, 35, 45), ?????????????array(27, 29, 37, 48), ?????????????array(32, 33, 39, 50)); printLN($mat); // This code is contributed by ajit? ?> ``` **輸出**: ``` 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 ```
                  <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>

                              哎呀哎呀视频在线观看