Gaussian Elimination. The code is directly attached, this code is not brought back.
1 //Writer : Hsz %WJMZBMR%tourist%hzwer 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 #include<set> 9 #include<stack> 10 #include<vector> 11 #include<cstdlib> 12 #include<algorithm> 13 #define LL long long 14 using namespace std; 15 int n,w[105]; 16 const double eps=1e- 8 ; 17 double a[ 105 ][ 105 ],b[ 105 ],v[ 105 ]; 18 void gauss() { 19 for ( int i= 1 ; i<=n; i++ ) {/ /enumerate each equation and record the largest coefficient for each equation 20 int p= 0 ; 21 double mx= 0 ; 22 for ( int j= 1 ; j<=n; j++ ) 23 if (fabs(a[ i][j])-eps>mx) mx=fabs(a[i][j]),p= j; 24 if (! p) { 25 printf( " No Solution " ); 26 return ; 27 } 28 w[i]= p; 29 for ( int j= 1 ; j<=n; j++ ) 30 if (i!= j) { 31 double tt=a[j][p]/ a[i][p]; 32 for ( int k= 1 ; k<=n+ 1 ; k++ )//Remember to cancel the result on the right side of the equation 33 a[j][k]-=a[i][k]*tt; // Elimination, the matrix formed by the coefficients of this equation has only one 0 in each row, and only one in each column is 0. 34 } 35 } 36 for ( int i= 1 ; i<=n; i++) v [w[i]]=a[i][n+ 1 ]/ a[i][w[i]];//Record the answer with v[], w[] represents the position of the element that has not been eliminated . 37 for ( int i= 1 ; i<=n; i++) printf( " %.2lf\n " ,v[i]); 38 } 39 int main() { 40 cin>> n; 41 for ( int i = 1 ; i<=n; i++ ) 42 for (int j=1; j<=n+1; j++) 43 scanf("%lf",&a[i][j]); 44 45 gauss(); 46 return 0; 47 }