It's very well written, and the notes are very detailed and comprehensive.
Original Blog Address: http://www.cnblogs.com/kuangbin/archive/2012/09/01/2667044.html
#include <stdio.h>#include<algorithm>#include<iostream>#include<string.h>#include<math.h>using namespacestd;Const intmaxn= -;intA[MAXN][MAXN];//Augmented matrixintX[MAXN];//Solution SetBOOLFREE_X[MAXN];//whether the tag is an indeterminate variable/*void Debug (void) {int I, J; for (i = 0, i < equ; i++) {for (j = 0, J < var + 1; j + +) {cout << a[i][j] <&l T " "; } cout << Endl; } cout << Endl;}*/inlineintgcdintAintb) { intT; while(b!=0) {T=b; b=a%b; A=T; } returnA;} InlineintLcmintAintb) { returnA/GCD (A, b) *b;//first In addition to the rear multiply anti-overflow}//The Gaussian elimination method is the solution of the equation set (Gauss-jordan elimination). (-2 indicates a floating-point solution, but no integer solution,//-1 for no solution, 0 for the unique solution, greater than 0 for the infinite solution, and the number of free arguments returned)//there is a equ equation, var variable element. The number of augmented matrix rows is equ, 0 to Equ-1, and the number of columns is var+1, which is 0 to Var, respectively.intGauss (intEquint var){ inti,j,k; intMax_r;//The row with the highest absolute value for the current column. intCol//columns currently being processed intTA,TB; intLCM; inttemp; intFree_x_num; intFree_index; for(intI=0; i<=var; i++) {X[i]=0; Free_x[i]=true; } //converted to a stepped array.Col=0;//columns currently being processed for(k =0; k < equ && Col <var; k++,col++) {//enumerates the currently processed rows.//The row that finds the largest absolute value of the col column element is exchanged with the K line. (To reduce the error when dividing)Max_r=K; for(i=k+1; i<equ;i++) { if(ABS (A[i][col]) >abs (A[max_r][col]) max_r=i; } if(max_r!=k) {//interchange with section K. for(j=k;j<var+1; j + +) Swap (a[k][j],a[max_r][j]); } if(a[k][col]==0) {//indicates that the COL column K line below is all 0, then the next column of the current row is processed.k--; Continue; } for(i=k+1; i<equ;i++) {//enumerates the rows to be deleted. if(a[i][col]!=0) {LCM=LCM (ABS (A[i][col]), ABS (A[k][col)); Ta= lcm/ABS (A[i][col]); TB= lcm/ABS (A[k][col]); if(a[i][col]*a[k][col]<0) TB=-TB;//the case of a different number is added for(j=col;j<var+1; j + +) {A[i][j]= a[i][j]*ta-a[k][j]*TB; } } } } //Debug (); //1. The absence of solution: there is an augmented array of degeneracy (0, 0, ..., a) such a line (a! = 0). for(i = k; i < equ; i++) { //for Infinite Solutions, if you want to determine which ones are free, then the exchange in the elementary row transformation will be affected, then the exchange should be recorded. if(A[i][col]! =0)return-1; } //2. The case of Infinity: in the augmented array of Var * (var + 1) (0, 0, ..., 0) Such a line, that is to say, does not form a strict upper triangular array. //and the number of rows that appear is the number of free arguments. if(K <var) { //First, the free variable has var-k, that is, the indeterminate variable has at least var-k. for(i = k-1; I >=0; i--) { //line I will certainly not be (0, 0, ..., 0) in the case, as such a line is in line K to equ. //Similarly, line I must not be (0, 0, ..., a), A! = 0 case, such a non-solvable.Free_x_num =0;//used to determine the number of indeterminate arguments in the row, if more than 1 are not solved, they are still indeterminate arguments. for(j =0; J <var; J + +) { if(A[i][j]! =0&& Free_x[j]) free_x_num++, Free_index =J; } if(Free_x_num >1)Continue;//The determined variable cannot be solved. //The description is only an indeterminate variable free_index, then the variable can be solved, and the variable is determined.temp = a[i][var]; for(j =0; J <var; J + +) { if(A[i][j]! =0&& J! = Free_index) Temp-= a[i][j] *X[j]; } X[free_index]= Temp/a[i][free_index];//To find out the variable element.Free_x[free_index] =0;//the variable is deterministic. } return varK//There are var-k of free-variable elements. } //3. The only solution: a strict upper triangular array is formed in the augmented array of Var * (var + 1). //calculate the Xn-1, Xn-2 ... X0. for(i =var-1; I >=0; i--) {Temp= a[i][var]; for(j = i +1; J <var; J + +) { if(A[i][j]! =0) Temp-= a[i][j] *X[j]; } if(temp% a[i][i]! =0)return-2;//indicates that there is a floating-point number solution, but no integer solution.X[i] = temp/A[i][i]; } return 0;}intMainvoid) {freopen ("In.txt","R", stdin); Freopen ("OUT.txt","W", stdout); intI, J; intEquvar; while(SCANF ("%d%d", &equ, &var) !=EOF) {memset (A,0,sizeof(a)); for(i =0; i < equ; i++) { for(j =0; J <var+1; J + +) {scanf ("%d", &A[i][j]); } }//Debug (); intFree_num = Gauss (equ,var); if(Free_num = =-1) printf ("No solution!\n"); Else if(Free_num = =-2) printf ("with floating-point solution, no integer solution!\n"); Else if(Free_num >0) {printf ("infinitely many solutions! The number of free arguments is%d\n", Free_num); for(i =0; I <var; i++) { if(Free_x[i]) printf ("x%d is not sure, \ n", i +1); Elseprintf"x%d:%d\n", i +1, X[i]); } } Else { for(i =0; I <var; i++) {printf ("x%d:%d\n", i +1, X[i]); }} printf ("\ n"); } return 0;}
"Turn" Gaussian elimination template by Kuangbin