<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # zag-zag 方式打印矩陣 > 原文: [https://www.geeksforgeeks.org/print-matrix-zag-zag-fashion/](https://www.geeksforgeeks.org/print-matrix-zag-zag-fashion/) 給定 n 行和 m 個列的 2D 數組矩陣。 如圖所示,以 ZIG-ZAG 方式打印此矩陣。 ![matrix_zag-zag](https://img.kancloud.cn/2e/16/2e167dafe08fe8d1878b23981e652a8f_656x353.png) **示例**: ``` Input: 1 2 3 4 5 6 7 8 9 Output: 1 2 4 7 5 3 6 8 9 ``` **C++ 代碼的方法** 這種方法很簡單。 只需一次遍歷每個對角線元素,然后根據上一個匹配項更改方向即可。 **Python3 代碼的方法** 這種方法很簡單。 在以通常的方式行進矩陣時,如果元素 i 和 j 的總和分別為偶數或奇數,則根據元素索引總和的奇偶校驗,將該特定元素添加到列表的開頭或末尾 。 按原樣打印解決方案列表。 ## C++ ```cpp /* C++ Program to print matrix in Zig-zag pattern*/ #include <iostream> using namespace std; #define C 3 // Utility function to print matrix // in zig-zag form void zigZagMatrix(int arr[][C], int n, int m) { ????int row = 0, col = 0; ????// Boolean variable that will true if we ????// need to increment 'row' value otherwise ????// false- if increment 'col' value ????bool row_inc = 0; ????// Print matrix of lower half zig-zag pattern ????int mn = min(m, n); ????for (int len = 1; len <= mn; ++len) { ????????for (int i = 0; i < len; ++i) { ????????????cout << arr[row][col] << " "; ????????????if (i + 1 == len) ????????????????break; ????????????// If row_increment value is true ????????????// increment row and decrement col ????????????// else decrement row and increment ????????????// col ????????????if (row_inc) ????????????????++row, --col; ????????????else ????????????????--row, ++col; ????????} ????????if (len == mn) ????????????break; ????????// Update row or col value according ????????// to the last increment ????????if (row_inc) ????????????++row, row_inc = false; ????????else ????????????++col, row_inc = true; ????} ????// Update the indexes of row and col variable ????if (row == 0) { ????????if (col == m - 1) ????????????++row; ????????else ????????????++col; ????????row_inc = 1; ????} ????else { ????????if (row == n - 1) ????????????++col; ????????else ????????????++row; ????????row_inc = 0; ????} ????// Print the next half zig-zag pattern ????int MAX = max(m, n) - 1; ????for (int len, diag = MAX; diag > 0; --diag) { ????????if (diag > mn) ????????????len = mn; ????????else ????????????len = diag; ????????for (int i = 0; i < len; ++i) { ????????????cout << arr[row][col] << " "; ????????????if (i + 1 == len) ????????????????break; ????????????// Update row or col value according ????????????// to the last increment ????????????if (row_inc) ????????????????++row, --col; ????????????else ????????????????++col, --row; ????????} ????????// Update the indexes of row and col variable ????????if (row == 0 || col == m - 1) { ????????????if (col == m - 1) ????????????????++row; ????????????else ????????????????++col; ????????????row_inc = true; ????????} ????????else if (col == 0 || row == n - 1) { ????????????if (row == n - 1) ????????????????++col; ????????????else ????????????????++row; ????????????row_inc = false; ????????} ????} } // Driver code int main() { ????int matrix[][3] = { { 1, 2, 3 }, ????????????????????????{ 4, 5, 6 }, ????????????????????????{ 7, 8, 9 } }; ????zigZagMatrix(matrix, 3, 3); ????return 0; } ``` ## Java ```java /* Java Program to print matrix in Zig-zag pattern*/ class GFG { ??static final int C = 3; ??// Utility function to print matrix ??// in zig-zag form ??static void zigZagMatrix(int arr[][], int n, int m) { ????int row = 0, col = 0; ????// Boolean variable that will true if we ????// need to increment 'row' value otherwise ????// false- if increment 'col' value ????boolean row_inc = false; ????// Print matrix of lower half zig-zag pattern ????int mn = Math.min(m, n); ????for (int len = 1; len <= mn; ++len) { ??????for (int i = 0; i < len; ++i) { ????????System.out.print(arr[row][col] + " "); ????????if (i + 1 == len) ??????????break; ????????// If row_increment value is true ????????// increment row and decrement col ????????// else decrement row and increment ????????// col ????????if (row_inc) { ??????????++row; ??????????--col; ????????} else { ??????????--row; ??????????++col; ????????} ??????} ??????if (len == mn) ????????break; ??????// Update row or col valaue according ??????// to the last increment ??????if (row_inc) { ????????++row; ????????row_inc = false; ??????} else { ????????++col; ????????row_inc = true; ??????} ????} ????// Update the indexes of row and col variable ????if (row == 0) { ??????if (col == m - 1) ????????++row; ??????else ????????++col; ??????row_inc = true; ????} else { ??????if (row == n - 1) ????????++col; ??????else ????????++row; ??????row_inc = false; ????} ????// Print the next half zig-zag pattern ????int MAX = Math.max(m, n) - 1; ????for (int len, diag = MAX; diag > 0; --diag) { ??????if (diag > mn) ????????len = mn; ??????else ????????len = diag; ??????for (int i = 0; i < len; ++i) { ????????System.out.print(arr[row][col] + " "); ????????if (i + 1 == len) ??????????break; ????????// Update row or col value according ????????// to the last increment ????????if (row_inc) { ??????????++row; ??????????--col; ????????} else { ??????????++col; ??????????--row; ????????} ??????} ??????// Update the indexes of row and col variable ??????if (row == 0 || col == m - 1) { ????????if (col == m - 1) ??????????++row; ????????else ??????????++col; ????????row_inc = true; ??????} ??????else if (col == 0 || row == n - 1) { ????????if (row == n - 1) ??????????++col; ????????else ??????????++row; ????????row_inc = false; ??????} ????} ??} ??// Driver code ??public static void main(String[] args) { ????int matrix[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; ????zigZagMatrix(matrix, 3, 3); ??} } // This code is contributed by Anant Agarwal. ``` ## Python3 ```py # Program to print matrix in Zig-zag pattern matrix =[ ????????????[ 1, 2, 3,], ????????????[ 4, 5, 6 ], ????????????[ 7, 8, 9 ], ????????] rows=3 columns=3 solution=[[] for i in range(rows+columns-1)] for i in range(rows): ????for j in range(columns): ????????sum=i+j ????????if(sum%2 ==0): ????????????#add at beginning ????????????solution[sum].insert(0,matrix[i][j]) ????????else: ????????????#add at end of the list ????????????solution[sum].append(matrix[i][j]) # print the solution as it as for i in solution: ????for j in i: ????????print(j,end=" ") ``` ## C# ```cs // C# Program to print matrix // in Zig-zag pattern using System; class GFG { ????static int C = 3; ????// Utility function to print ????// matrix in zig-zag form ????static void zigZagMatrix(int[, ] arr, int n, int m) ????{ ????????int row = 0, col = 0; ????????// Boolean variable that will ????????// true if we need to increment ????????// 'row' valueotherwise false- ????????// if increment 'col' value ????????bool row_inc = false; ????????// Print matrix of lower half ????????// zig-zag pattern ????????int mn = Math.Min(m, n); ????????for (int len = 1; len <= mn; ++len) { ????????????????for (int i = 0; i < len; ++i) { ????????????????Console.Write(arr[row, col] + " "); ????????????????if (i + 1 == len) ????????????????????break; ????????????????// If row_increment value is true ????????????????// increment row and decrement col ????????????????// else decrement row and increment ????????????????// col ????????????????if (row_inc) { ????????????????????++row; ????????????????????--col; ????????????????} ????????????????else { ????????????????????--row; ????????????????????++col; ????????????????} ????????????} ????????????if (len == mn) ????????????????break; ????????????// Update row or col valaue ????????????// according to the last ????????????// increment ????????????if (row_inc) { ????????????????????++row; ????????????????????row_inc = false; ????????????} ????????????else { ????????????????++col; ????????????????row_inc = true; ????????????} ????????} ????????// Update the indexes of row ????????// and col variable ????????if (row == 0) { ????????????if (col == m - 1) ????????????????++row; ????????????else ????????????????++col; ????????????row_inc = true; ????????} ????????else { ????????????if (row == n - 1) ????????????????++col; ????????????else ????????????????++row; ????????????row_inc = false; ????????} ????????// Print the next half ????????// zig-zag pattern ????????int MAX = Math.Max(m, n) - 1; ????????for (int len, diag = MAX; diag > 0; --diag) { ????????????if (diag > mn) ????????????????????len = mn; ????????????else ????????????????len = diag; ????????????for (int i = 0; i < len; ++i) { ????????????????????Console.Write(arr[row, col] + " "); ????????????????if (i + 1 == len) ????????????????????break; ????????????????// Update row or col value ????????????????// according to the last ????????????????// increment ????????????????if (row_inc) { ????????????????????????++row; ????????????????????????--col; ????????????????} ????????????????else { ????????????????????++col; ????????????????????--row; ????????????????} ????????????} ????????????// Update the indexes of ????????????// row and col variable ????????????if (row == 0 || col == m - 1) { ????????????????if (col == m - 1) ????????????????????++row; ????????????????else ????????????????????++col; ????????????????row_inc = true; ????????????} ????????????else if (col == 0 || row == n - 1) { ????????????????if (row == n - 1) ????????????????????++col; ????????????????else ????????????????????++row; ????????????????row_inc = false; ????????????} ????????} ????} ????// Driver code ????public static void Main() ????{ ????????int[, ] matrix = { { 1, 2, 3 }, ????????????????????????{ 4, 5, 6 }, ????????????????????????{ 7, 8, 9 } }; ????????zigZagMatrix(matrix, 3, 3); ????} } // This code is contributed by vt_m. ``` ## PHP ```php <?php // PHP Program to print? // matrix in Zig-zag pattern $C = 3; // Utility function? // to print matrix // in zig-zag form function zigZagMatrix($arr, ??????????????????????$n, $m) { ????$row = 0; $col = 0; ????// Boolean variable that ????// will true if we need? ????// to increment 'row'? ????// value otherwise false- ????// if increment 'col' value ????$row_inc = false; ????// Print matrix of lower ????// half zig-zag pattern ????$mn = min($m, $n); ????for ($len = 1;? ?????????$len <= $mn; $len++)? ????{ ????????for ($i = 0;? ?????????????$i < $len; $i++)? ????????{ ????????????echo ($arr[$row][$col]." "); ????????????if ($i + 1 == $len) ????????????????break; ????????????// If row_increment value? ????????????// is true increment row? ????????????// and decrement col else? ????????????// decrement row and? ????????????// increment col ????????????if ($row_inc) ????????????{ ????????????????$row++; $col--; ????????????} ????????????else ????????????{ ????????????????$row--; $col++; ????????????} ????????} ????????if ($len == $mn) ????????????break; ????????// Update row or col ????????// value according ????????// to the last increment ????????if ($row_inc)? ????????{ ????????????++$row; $row_inc = false; ????????} ????????else? ????????{ ????????????++$col; $row_inc = true; ????????} ????} ????// Update the indexes of ????// row and col variable ????if ($row == 0)? ????{ ????????if ($col == $m - 1) ????????????++$row; ????????else ????????????++$col; ????????$row_inc = 1; ????} ????else? ????{ ????????if ($row == $n - 1) ????????????++$col; ????????else ????????????++$row; ????????$row_inc = 0; ????} ????// Print the next half ????// zig-zag pattern ????$MAX = max($m, $n) - 1; ????for ($len, $diag = $MAX; ?????????$diag > 0; --$diag)? ????{ ????????if ($diag > $mn) ????????????$len = $mn; ????????else ????????????$len = $diag; ????????for ($i = 0;? ?????????????$i < $len; ++$i) ????????{ ????????????echo($arr[$row][$col] . " "); ????????????if ($i + 1 == $len) ????????????????break; ????????????// Update row or col? ????????????// value according to ????????????// the last increment ????????????if ($row_inc) ????????????{ ????????????????++$row; --$col; ????????????} ????????????else ????????????{ ????????????????++$col; --$row; ????????????} ????????} ????????// Update the indexes of ????????// row and col variable ????????if ($row == 0 ||? ????????????$col == $m - 1)? ????????{ ????????????if ($col == $m - 1) ????????????????++$row; ????????????else ????????????????++$col; ????????????$row_inc = true; ????????} ????????else if ($col == 0 ||? ?????????????????$row == $n - 1)? ????????{ ????????????if ($row == $n - 1) ????????????????++$col; ????????????else ????????????????++$row; ????????????$row_inc = false; ????????} ????} } // Driver code $matrix = array(array(1, 2, 3), ????????????????array(4, 5, 6), ????????????????array(7, 8, 9)); zigZagMatrix($matrix, 3, 3); // This code is contributed by? // Manish Shaw(manishshaw1) ?> ``` **輸出**: ``` 1 2 4 7 5 3 6 8 9 ``` **時間復雜度**: O(n * m) **輔助空間**:`O(1)` * * * * * *
                  <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>

                              哎呀哎呀视频在线观看