雷达智富

首页 > 内容 > 程序笔记 > 正文

程序笔记

以蛇形模式打印矩阵的Python程序

2024-08-19 27

在本文中,我们将学习一个以蛇形模式打印矩阵的 python 程序。

假设我们取了 n x n 矩阵。我们现在将使用下面提到的方法以蛇形模式打印输入矩阵。

使用的方法

以下是用于完成此任务的各种方法 -

使用嵌套的 for 循环

使用切片反转交替行

直觉

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

方法 1: 使用嵌套的 for 循环

算法(步骤)

以下是执行所需任务要遵循的算法/步骤。−

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

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

创建一个函数 printSnakePattern(),用于通过接受输入矩阵作为参数来打印蛇模式的矩阵。

使用 global 关键字使行和列变量成为全局变量。

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

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

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

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

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

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

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

以下程序使用嵌套的 for 循环以蛇模式打印输入矩阵 -

# 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]

参数

开始 − 索引 从哪里开始

结束 − 结束索引

步长 − 之间要采取的跳跃次数,即步长

以下程序使用切片以蛇形模式打印输入矩阵 -

# 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

结论

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

更新于:1个月前
赞一波!3

文章评论

全部评论