Python程序打印矩阵的蛇形图案


在这篇文章中,我们将学习一个Python程序,用于以蛇形图案打印矩阵。

假设我们已经得到了一个n x n的矩阵。我们将使用下面提到的方法打印输入矩阵的蛇形图案。

使用的方法

以下是完成此任务的各种方法:

  • 使用嵌套for循环

  • 使用切片反转交替行

思路

我们将遍历矩阵的所有行。对于每一行,我们将检查它是奇数还是偶数。如果行数是偶数,则从左到右打印矩阵;否则,从右到左打印矩阵。

方法1:使用嵌套for循环

算法(步骤)

以下是执行所需任务的算法/步骤:

  • 创建一个变量来存储矩阵的行数。

  • 创建一个变量来存储矩阵的列数。

  • 创建一个函数printSnakePattern(),通过接受输入矩阵作为参数来打印蛇形图案的矩阵。

  • 使用global关键字使rows和columns变量成为全局变量。

  • 使用for循环遍历矩阵的行。

  • 使用if条件语句检查当前行号是否为偶数。

  • 如果条件为真,则使用另一个嵌套for循环遍历当前行的所有列。

  • 如果当前行是偶数,则从左到右打印矩阵行。

  • 否则,如果当前行是奇数,则从右到左打印矩阵行。

  • 创建一个变量来存储输入矩阵并打印给定的矩阵。

  • 通过将输入矩阵作为参数传递给上面定义的printSnakePattern()函数来调用它。

示例

下面的程序使用嵌套for循环打印输入矩阵的蛇形图案:

Open Compiler
# initializing the number of rows of the matrix rows = 4 # initializing the number of columns of the matrix columns = 4 # creating a function for printing the matrix in # snake pattern accepting the input matrix as an argument. def printSnakePattern(inputMatrix): # making the rows and columns variables global global rows, columns # traversing through the rows of a matrix for m in range(rows): # checking whether the current row number is even if m % 2 == 0: # traversing through all the columns of the current row for n in range(columns): # printing from left to right if the current row is even print(inputMatrix[m][n], end=" ") # Else, printing from right to left if the current row is even else: # traversing from the end of the columns for n in range(columns - 1, -1, -1): print(inputMatrix[m][n], end=" ") # input matrix inputMatrix = [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] print("The Given Matrix is :") print(inputMatrix) # calling the above-defined printSnakePattern function # by passing the input matrix as an argument. print("Snake Pattern of the given Matrix is:") printSnakePattern(inputMatrix)

输出

执行上述程序后,将生成以下输出:

The Given Matrix is :
[[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]]
Snake Pattern of the given Matrix is:
3 4 5 6 80 60 40 10 1 9 7 8 15 14 20 40 

方法2:使用切片反转交替行

切片是一种常见的实践,程序员最常使用它来有效地解决问题。考虑一个Python列表。您必须切片列表才能访问列表元素的范围。使用冒号(:),一个简单的切片运算符,是一种实现此目的的方法。

语法

[start:stop:step]

参数

  • start - 开始索引

  • end - 结束索引

  • step - 步长

示例

下面的程序使用切片打印输入矩阵的蛇形图案:

Open Compiler
# input matrix inputMatrix = [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] # initializing the number of rows of a matrix rows = 4 # initializing the number of columns of a matrix columns = 4 # creating a function for printing the matrix in # snake pattern accepting the input matrix as an argument. def printSnakePattern(inputMatrix): # making the rows and columns variables global global rows, columns # traversing through the rows of a matrix for m in range(rows): # checking whether the current row number is even if m % 2 != 0: # Reversing the row using reverse slicing inputMatrix[m] = inputMatrix[m][::-1] # traversing through the rows of a matrix for m in range(rows): # traversing through all the columns of the current row for n in range(columns): # printing the corresponding element print(inputMatrix[m][n], end=' ') # input matrix inputMatrix = [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] print("The Given Matrix is :") print(inputMatrix) # calling the above-defined printSnakePattern function # by passing the input matrix as an argument. print("Snake Pattern of the given Matrix is:") printSnakePattern(inputMatrix)

输出

执行后,上述程序将生成以下输出:

The Given Matrix is :
[[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]]
The Snake Pattern of the given Matrix is:
3 4 5 6 80 60 40 10 1 9 7 8 15 14 20 40 

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

结论

在本文中,我们学习了如何使用两种不同的方法以蛇形方式打印给定的矩阵。我们学习了如何使用global关键字使变量成为全局变量。我们还学习了如何通过反向切片反转任何可迭代对象,包括列表、元组、字符串等。

更新于:2023年1月31日

2K+ 浏览量

开启你的职业生涯

通过完成课程获得认证

开始学习
广告