Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report