If you encounter the problem of solving the equation, it is not easy to write a formula for each unknown, but when we do not know the data range of the solution, the two-minute enumeration of what and did not make no difference, so here introduced the Gaussian elimination to answer this.
Gaussian elimination is simply a computer tailored to solve the N-ary equation set of the weapon, although in the algorithm contest is not the same as the addition and subtraction of the easy topic, but this as its basis, or need to discuss.
1. Basic concept
Gaussian elimination refers to the n-i of the sub-equation below it by the addition and subtraction method to 0, and then the process of solving all unknowns in turn.
2, plus and minus the elimination of Yuan
To facilitate the description, we arrange each formula according to the principle of the same unknown alignment (if some of the equations have unknown unknowns, then the coefficients are expressed in 0, that is, empty), and the unknowns are placed on the left, the constants on the right, in order to simplify the wording, here the determinant of each equation in terms of the coefficient of each unknown, In the figure of line I, Column J, is the coefficient of the J-unknowns of the first I equation, and the n+1 column represents a constant term (for convenience of description, assuming that all coefficients are nonzero, a zero is discussed later), as described below:
A[1][1] a[1][2] a[1][3] ... a[1][n] | C[1]
A[2][1] a[2][2] a[2][3] ... a[2][n] | C[2]
. .
. .
. .
A[N][1] a[n][2] a[n][3] ... a[n][n] | C[n]
Then take the first step:
A[1][1] a[1][2] a[1][3] ... a[1][n] | C[1]
0 a[2][2]*a[1][1]-a[1][2]*a[2][1] a[2][3]*a[1][1]-a[1][3]*a[2][1] ... | C[2]*A[1][1]-C[1]*A[2][1]
0 a[3][2]*a[1][1]-a[1][2]*a[3][1] a[3][3]*a[1][1]-a[1][3]*a[3][1] ... | C[3]*A[1][1]-C[1]*A[3][1]
. .
. .
It is very clear that the meaning of our operation, after the first operation, except the first equation, the coefficients of the first term of all equations are changed to 0, and the coefficients of each equation are updated. The next step is to make the second factor in addition to the first, two equations become 0, so repeat, until step n-1, you will be surprised to find that the nth equation only the nth unknown! And that's a one-dimensional equation! It is easy to get the value of X[n], then x[n]n into each of the above equation, and then merge it with the constant term, you will find that x[n-1] can also be solved by the first n-1 equation! So pushed up, you get the value of all unknowns!
3. Algorithm Analysis
With the above solution, it is easy to knock out the code, but there are also areas to note:
The situation without solution:
When there is an equation with all coefficients of 0 and the constant term is not 0 o'clock, the equations are non-solvable;
The No.2 has a condition with zero coefficient:
In the process of processing data, you may encounter a factor of zero, which can be skipped directly, because our purpose in this step can be done even if we skip it.
Here is the detailed code:
#include <cstdio>Const intMAXN = -+Ten;//It is assumed that a maximum of 100 yuan is required to solve the equationDoubleDAT[MAXN][MAXN],ANS[MAXN];//integer division can be problematic (in case it can't be divisible?). )intN;BOOLRT =1;//record whether there is a solutionvoidInit () {//the coefficients of the initialization, reading in the equationscanf"%d",&N); for(inti =1; I <= n;++i) { for(intj =1; J <= N +1;++j) {scanf ("%LF",&Dat[i][j]); } }}voidWork () {//processing the resulting data for(inti =1; I <= n;++i) { intj =i; while(dat[j][i]==0) j + +;//To prevent some of the partial coefficients to be zero, then use the first dat[j][i] below J to update the formula below it. for(intK = j+1; k <= N +1;++k) { for(intm = i;m <= n +1;++m) {Dat[k][m]= Dat[k][m]*dat[j][i]-dat[j][m]*Dat[k][i]; } } } if(dat[n+1][n+1]) RT =0;//if all coefficients are zero and the right side of the equation is not zero, the equation Group has no solution}voidSolv () {if(!RT)return; for(inti = N;i >=1;---I) {//solve from bottom to top if(Dat[i][i] = =0)Continue; Ans[i]= dat[i][n+1]/Dat[i][i]; for(intj = I1; J >=1;--j) {Dat[j][n+1]-= dat[j][i]*Ans[i]; } }}voidPrnt () {//Output Answer if(!RT) {printf ("No answer!");return;} for(inti =1; I <= n;++i) {printf ("x%d", i); if(Ans[i]! = (1<< -) {printf ("=%.2lf", Ans[i]); } Else{printf ("is unknown"); } printf ("\ n"); }}intMain () {init ();//InitializeWork ();//working with DataSolv ();//solvingPrnt ();//Output return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Gaussian elimination Element (i)--basic concept and additive and subtraction element