epsilon = pow(10,-10)
g = []
for _ in range(int(input())):
g.append([*map(float,input().split())])
n,m = len(g),len(g[0])
row = 0
for col in range(n):
max_row = row
# 找出剩下行最大列值所在的行
for i in range(row,n):
if abs(g[i][col]) > abs(g[max_row][col]):
max_row = i
# 如果等于0,省去下面的消元,直接跳过
if abs(g[max_row][col]) < epsilon:
continue
# 将目标行与第row行进行交换
g[max_row],g[row] = g[max_row],g[row]
# 倒序枚举,将第row行同时除以g[row][col],使得g[row][col] = 1
for j in range(m - 1,col - 1,-1):
g[row][j] /= g[row][col]
# row行以下的行的第col列全部变成0,构造目标三角0
for i in range(row + 1,n):
if abs(g[i][col]) > epsilon:
for j in range(m - 1,col - 1,-1):
g[i][j] -= g[row][j] * g[i][col]
row += 1
# print(*g,sep='\n')
# n个方程组可以解有n个未知数的方程
# 如果有效方程组数小于未知数个数,那么可能有无数个解或无解
if row < n:
for i in range(row,n):
# 出 现 0 = !0 的 情 况 !
if abs(g[i][m - 1]) > epsilon:
print("No solution")
exit()
print("Infinite group solutions")
else:
# 精髓:从最后一行往第一行推,将答案记在最后一列
for i in range(n - 1,-1,-1):
for j in range(i + 1,n):
g[i][m - 1] -= g[j][m - 1] * g[i][j]
for i in range(n):
print("%.2f" % g[i][m - 1])
# print(*g,sep='\n')
标签:max,range,abs,print,col,高斯消,row
From: https://www.cnblogs.com/gebeng/p/18111399