Get the App
SLTechnology News&Howtos  ›  Development  › 

How to give a square or rectangular matrix matrix and realize zigzag printing

Shulou Source: shulou.com Published: 2022-06-03 11:28:08 09月17日 Update

Today, I will talk to you about how to give a square or rectangular matrix matrix and how to achieve zigzag printing. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Two for loops are nested.

The outer loop. First iterate through the first column, and then iterate through the last row that does not contain the column number 0. Change the flag bit every time you cycle.

Inner circulation. Judging by the sign bit, from lower left to upper right, or from upper right to lower left.

The code is written in golang. The code is as follows:

Package main

Import "fmt"

Func main () {arr: = [] [] int {{0,1,2}, {3,4,5}, {6,7,8}} printMatrixZigZag (arr)} func printMatrixZigZag (matrix [] [] int) {row: = len (matrix) col: = len (matrix [0]) fromUp: = false / / traverse the first column for I: = 0; I

< row; i++ { if fromUp { //找最右上的位置 j := 0 for ; i-j >

= 0 & & j

< col; j++ { } j-- //右上到左下 for ; j >

= 0; fmt.Print-{fmt.Print (Matrix [I-j] [0qj], ")}} else {/ / bottom left to upper right for j: = 0; iMub j > = 0 & & j

< col; j++ { fmt.Print(matrix[i-j][0+j], " ") } } fromUp = !fromUp } //遍历最后一行 for j := 1; j < col; j++ { if fromUp { //找最右上的位置 i := 0 for ; row-1-i >

= 0 & & jpragi

< col; i++ { } i-- //右上到左下 for ; i >

Else {/ / bottom left to upper right for I: = 0; row-1-i > = 0 & & Jaimi < col FromUp + {fmt.Print (Matrix [row-1 muri] [Jubi], "")}} fromUp =! fromUp}}

The implementation results are as follows:

***

[left God java Code] (https://github.com/algorithmzuo/algorithmbasic2020/blob/master/src/class40/Code07_ZigZagPrintMatrix.java)

[comments] (https://user.qzone.qq.com/3182319461/blog/1617318990)

After reading the above, do you have any further understanding of how to give a square or rectangular matrix matrix and how to implement zigzag printing? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Loops code content jacks squares squares matrices rectangles lines positions flags two inner layer outer layer more knowledge articles results industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Shulou Information Linux Xiaomi