acwing:https://www.acwing.com/problem/content/885/

高斯消元求解线性方程组

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 110;
const double eps = 1e-8;
LL n;
double a[N][N];
LL gauss(){
	LL c, r;
	for (c = 0, r = 0; c < n; c ++ ){
		LL t = r;
		for (int i = r; i < n; i ++ )	//找到绝对值最大的行 
			if (fabs(a[i][c]) > fabs(a[t][c]))
				t = i;
		if (fabs(a[t][c]) < eps) continue;
		for (int j = c; j < n + 1; j ++ ) swap(a[t][j], a[r][j]);	//将绝对值最大的一行换到最顶端
		for (int j = n; j >= c; j -- ) a[r][j] /= a[r][c];	//将当前行首位变成 1
		for (int i = r + 1; i < n; i ++ )	//将下面列消成 0 
			if (fabs(a[i][c]) > eps)
				for (int j = n; j >= c; j -- )
					a[i][j] -= a[r][j] * a[i][c];
		r ++ ;
	}
	if (r < n){
		for (int i = r; i < n; i ++ )
			if (fabs(a[i][n]) > eps)
				return 2;
		return 1;
	}
	for (int i = n - 1; i >= 0; i -- )
		for (int j = i + 1; j < n; j ++ )
			a[i][n] -= a[i][j] * a[j][n];
	return 0;
}
int main(){
	cin >> n;
	for (int i = 0; i < n; i ++ )
		for (int j = 0; j < n + 1; j ++ )
			cin >> a[i][j];
	LL t = gauss();
	if (t == 0){
		for (int i = 0; i < n; i ++ ){
			if (fabs(a[i][n]) < eps) a[i][n] = abs(a[i][n]);
			printf("%.2lf\n", a[i][n]);
		}
	}
	else if (t == 1) cout << "Infinite group solutions\n";
	else cout << "No solution\n";
	return 0;
}
posted on 2022-04-29 20:02  Hamine  阅读(23)  评论(0编辑  收藏  举报