题解 | 使用拉普拉斯展开式的 4x4 矩阵的行列式
使用拉普拉斯展开式的 4x4 矩阵的行列式
https://www.nowcoder.com/practice/5b3331a8000c427f81cbe2aef7af36cd
import numpy as np
def determinant_4x4(A) :
A=np.array(A)
if A.shape[0]==2:
return A[0][0]*A[1][1]-A[0][1]*A[1][0]
else:
s=0
for i in range(A.shape[0]):
k=np.delete(np.arange(A.shape[0]),i)
s+=(-1)**i*A[0][i]*determinant_4x4(A[1:A.shape[0],k])
return s
# 主程序
if __name__ == "__main__":
# 输入矩阵
matrix_inputa = input()
# 处理输入
import ast
matrix = ast.literal_eval(matrix_inputa)
# 调用函数计算逆矩阵
output = determinant_4x4(matrix)
# 输出结果
print(output)


查看9道真题和解析