AcWing算法基础课笔记——高斯消元

高斯消元

用来求解方程组
a 11 x 1 + a 12 x 2 + ⋯ + a 1 n x n = b 1 a 21 x 1 + a 22 x 2 + ⋯ + a 2 n x n = b 2 … a n 1 x 1 + a n 2 x 2 + ⋯ + a n n x n = b n a_{11} x_1 + a_{12} x_2 + \dots + a_{1n} x_n = b_1\\ a_{21} x_1 + a_{22} x_2 + \dots + a_{2n} x_n = b_2\\ \dots \\ a_{n1} x_1 + a_{n2} x_2 + \dots + a_{nn} x_n = b_n\\ a11x1+a12x2++a1nxn=b1a21x1+a22x2++a2nxn=b2an1x1+an2x2++annxn=bn
输入是 n × ( n − 1 ) n \times (n -1 ) n×(n1)的矩阵。

对方程组进行以下三种初等行列变换后,方程的解不变:

  1. 把某一行乘以一个非零的数
  2. 交换某2行
  3. 把某行的若干倍加到另一行上去

因此,对任意一个方程组,可以把它变成倒三角形式:
a 11 x 1 + a 12 x 2 + ⋯ + a 1 n x n = b 1 a 22 x 2 + ⋯ + a 2 n x n = b 2 … a ( n − 1 ) ( n − 1 ) x n − 1 + a ( n − 1 ) n x n = b n − 1 a n n x n = b n a_{11}x_1 + a_{12}x_2+\dots +a_{1n}x_n = b_1 \\ a_{22}x_2 + \dots + a_{2n} x_n = b_2 \\ \dots \\ a_{(n-1)(n-1)}x_{n-1} +a_{(n-1)n}x_{n} = b_{n-1}\\ a_{nn}x_n = b_n a11x1+a12x2++a1nxn=b1a22x2++a2nxn=b2a(n1)(n1)xn1+a(n1)nxn=bn1annxn=bn
有三种情况:

  1. 完美阶梯型——唯一解
  2. 0 = 非零 ———无解
  3. 0 = 0 ——无穷多组解

高斯消元步骤:

枚举每一列c:

  1. 找到绝对值最大的一行
  2. 将该行换到最上面
  3. 将该行第一个数变成1
  4. 将下面所有行的第c列消成0

题目

详见:https://www.acwing.com/problem/content/description/885/

输入一个包含 n 个方程 n 个未知数的线性方程组。

方程组中的系数为实数。

求解这个方程组。

下图为一个包含 m 个方程 n 个未知数的线性方程组示例:

AcWing算法基础课笔记——高斯消元_第1张图片

输入格式

第一行包含整数 n。

接下来 n 行,每行包含 n+1 个实数,表示一个方程的 n 个系数以及等号右侧的常数。

输出格式

如果给定线性方程组存在唯一解,则输出共 n 行,其中第 i 行输出第 i 个未知数的解,结果保留两位小数。

注意:本题有 SPJ,当输出结果为 0.00 时,输出 -0.00 也会判对。在数学中,一般没有正零或负零的概念,所以严格来说应当输出 0.00,但是考虑到本题作为一道模板题,考察点并不在于此,在此处卡住大多同学的代码没有太大意义,故增加 SPJ,对输出 -0.00 的代码也予以判对。

如果给定线性方程组存在无数解,则输出 Infinite group solutions

如果给定线性方程组无解,则输出 No solution

数据范围

1≤n≤100,
所有输入系数以及常数均保留两位小数,绝对值均不超过 100。

输入样例:
3
1.00 2.00 -1.00 -6.00
2.00 1.00 -3.00 -9.00
-1.00 -1.00 2.00 7.00
输出样例:
1.00
-2.00
3.00

代码

#include
#include
#include

using namespace std;

const int N = 110;
const double eps = 1e-6;

int n;
double a[N][N];

int gauss() {
	int c, r;
	for(c = 0, r = 0; c < n; c ++ ) {
		// 找到绝对值最大的一行 t 
		int 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; //如果第t行为0,结束
		
		//将该行换到最上面 
		for(int i = c; i <= n; i ++ ) swap(a[t][i], a[r][i]);  
		
		//将该行的第c位设为1(前面都为0) 
		for(int i = n; i >= c; i --) a[r][i] /= a[r][c];
		
		//将下面所有行的第c列消成0
		//也就是从r + 1行开始,对于第i行,第i行第c个位置a[i][c]如果不为0的话,就要消成0
		// a[i][c]消成0 : a[i][c] -= a[r][c] * a[i][c]     a[r][c]为1
		// 那么其他所有列: a[i][j] -= a[r][j] * a[i][c]
		for(int i = r + 1; i < n; i ++ ) {
			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; //有无穷多组解 
	}
	
	//求解唯一解 
	//从第n - 1 行开始往上,遍历每一行
	//对于第i行,它的解是a[i][n]的值 
	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; j ++ ) {
			cin >> a[i][j];
		}
	}
	
	int t = gauss();
	if(t == 0) {
		for(int i = 0; i < n; i ++ ) printf("%.2lf\n", a[i][n]);
	}
	else if (t == 1) puts("Infinite group solutions");
	else puts("No solution");
	
	return 0;
}

你可能感兴趣的:(AcWing,算法,笔记,数论)