模板-高斯消元
高斯-约旦消元法
int n; double a[maxn][maxn]; void Gauss() { scanf("%d", &n); for (int i = 1; i <=n; i++) { for (int j = 1; j <=n + 1; j++) { scanf("%lf", &a[i][j]); } } for (int i = 1; i <= n; i++) { //枚举列(项) int max = i; for (int j = i + 1; j <= n; j++) { if (fabs(a[j][i]) > fabs(a[maxn][i])) max = j; } for (int j = 1; j <= n + 1; j++) { swap(a[i][j], a[max][j]); } if (!a[i][i]) { //最大值为0说明该列都为0,无解 printf("No solution"); return; } for (int j = 1; j <= n; j++) { //每一项都减去一个数 if (j != i) { double tmp = a[j][i] / a[i][i]; for (int k = i + 1; k <= n + 1; k++) { a[j][k] -= a[i][k] * tmp; } } } } for (int i = 1; i <= n; i++) { printf("%.2f\n", a[i][n + 1] / a[i][i]); } }