--- Python 3.7 ---
def MatrixRead(Rows, RowLength, ContType):
for row in range(Rows):
print('Enter line ' + str(row))
buff = []
[buff.append(ContType(input())) for _ in range(RowLength)]
yield buff
def MatrixSwap(Matrix, OldPos:tuple, NewPos: tuple):
Matrix[OldPos[0]][OldPos[1]], Matrix[OldPos[1]][OldPos[0]] = Matrix[NewPos[1]][NewPos[0]], Matrix[NewPos[0]][NewPos[1]]
return Matrix
def MatrixPrint(Matrix, end = '\n'):
for Row in Matrix:
print(*Row, end ='\n')
print(end)
def main():
N = int(input()) #Matrix Size
Matr =list( MatrixRead(N, N, int))
MatrixPrint(Matr) #For check
for row in range(N):
for col in range(N):
SecDiagPos = (row, N-col-1)
MainDiagPos = (row, col)
Matr = MatrixSwap(Matr, MainDiagPos, SecDiagPos)
MatrixPrint(Matr)
if __name__ == '__main__':
main()
Объяснение:
--- Python 3.8.3 ---
def MatrixPrint(Matr):
matrixstring = ''
rows = len(Matr)
cols = len(Matr[0])
for row in range(rows):
for col in range(cols):
matrixstring += str(Matr[row][col]) + ' '
matrixstring += '\n'
print(matrixstring)
n = int(input())
Matr = [[int(i) for i in input().split(maxsplit=n)] for i in range(n)]
for i in range(n):
Matr[i][i], Matr[i][n-i-1] = Matr[i][n-i-1], Matr[i][i]
if __name__ == "__main__":
--- Python 3.7 ---
def MatrixRead(Rows, RowLength, ContType):
for row in range(Rows):
print('Enter line ' + str(row))
buff = []
[buff.append(ContType(input())) for _ in range(RowLength)]
yield buff
def MatrixSwap(Matrix, OldPos:tuple, NewPos: tuple):
Matrix[OldPos[0]][OldPos[1]], Matrix[OldPos[1]][OldPos[0]] = Matrix[NewPos[1]][NewPos[0]], Matrix[NewPos[0]][NewPos[1]]
return Matrix
def MatrixPrint(Matrix, end = '\n'):
for Row in Matrix:
print(*Row, end ='\n')
print(end)
def main():
N = int(input()) #Matrix Size
Matr =list( MatrixRead(N, N, int))
MatrixPrint(Matr) #For check
for row in range(N):
for col in range(N):
SecDiagPos = (row, N-col-1)
MainDiagPos = (row, col)
Matr = MatrixSwap(Matr, MainDiagPos, SecDiagPos)
MatrixPrint(Matr)
if __name__ == '__main__':
main()
Объяснение:
--- Python 3.8.3 ---
def MatrixPrint(Matr):
matrixstring = ''
rows = len(Matr)
cols = len(Matr[0])
for row in range(rows):
for col in range(cols):
matrixstring += str(Matr[row][col]) + ' '
matrixstring += '\n'
print(matrixstring)
def main():
n = int(input())
Matr = [[int(i) for i in input().split(maxsplit=n)] for i in range(n)]
for i in range(n):
Matr[i][i], Matr[i][n-i-1] = Matr[i][n-i-1], Matr[i][i]
MatrixPrint(Matr)
if __name__ == "__main__":
main()
Объяснение: