于这个问答内容,以下是完善且全面的答案:
n = int(input("请输入矩阵的维度n: "))
matrix = []
for i in range(n):
row = []
for j in range(n):
value = int(input(f"请输入矩阵元素[{i}][{j}]: "))
row.append(value)
matrix.append(row)
print("矩阵:")
for row in matrix:
print(row)
def reshape_matrix(matrix):
n = len(matrix)
for i in range(n):
for j in range(n):
if i != j:
matrix[i][j] = 0
return matrix
res = reshape_matrix(matrix)
print("重塑后的对角矩阵:")
for row in res:
print(row)
领取专属 10元无门槛券
手把手带您无忧上云