P3389 [TEMPLATE] Gaussian elimination method, p3389 template Gaussian elimination
Background
Gauss Element
Description
Given a linear equations and solving them
Input/Output Format
Input Format:
The first line is a positive integer nn.
The second to n + 1n + 1 rows have n + 1n + 1 integers, which are a_1, a_2 \ cdots a_na1, a2 between an and bb, representing a group of equations.
Output Format:
N rows in total, one number per row, and the second behavior x_ixi (retain 2 decimal places)
If No unique Solution exists, output "No Solution" in the first line ".
Input and Output sample input sample #1:
31 3 4 51 4 7 39 3 2 2
Output sample #1:
-0.975.18-2.39
Description
1 \ leq n \ leq 100, \ left | a_ I \ right | \ leq {10} ^ 4, \ left | B \ right | \ leq {10} ^ 41 ≤ n ≤ 100, half ai ≤ 104, half B ≤ 104
I wanted to study the matrix in depth ,,
I don't know how to study the Gaussian elimination element ,....
Gaussian elimination method is really a god (bao) odd (li) thing,
I tried to sort it out and found that I would not write a matrix in the blog garden,
1 # include <cstdio> 2 # include <cstring> 3 # include <cmath> 4 # include <algorithm> 5 // # define Matrix double 6 using namespace std; 7 const int MAXN = 101; 8 typedef double Matrix [MAXN] [MAXN]; 9 inline void read (int & n) 10 {char c = getchar (); bool flag = 0; 11 while (c <'0' | c> '9') c = '-'? Flag = 1, c = getchar (): c = getchar (); 12 while (c> = '0' & c <= '9 ') n = n * 10 + c-48, c = getchar (); flag = 1? N =-n: n = n;} 13 int n; 14 Matrix a; 15 void debug () 16 {17/* printf ("******************************** \ n "); 18 for (int I = 1; I <= n; I ++) 19 {20 for (int j = 1; j <= n + 1; j ++) printf ("%. 2lf ", a [I] [j]); 21 printf (" \ n "); 22} */23} 24 void gauss_elimination (int n) 25 {26 int r; // The maximum value to be selected 27 for (int I = 1; I <= n; I ++) 28 {29 r = I; 30 for (int j = I + 1; j <= n; j ++) // enumerate 31 rows after fabs (a [j] [I])> fabs (a [r] [I]) r = j; 32 debug (); 33 if (r! = I) swap (a [r], a [I]); 34 debug (); 35 if (! A [I] [I]) 36 {37 printf ("No Solution \ n"); 38 return; 39} 40 for (int k = I + 1; k <= n; k ++) // offset from the following 41 {42 double f = a [k] [I]/a [I] [I]; // simulate the artificial consumption of RMB 43 for (int j = I; j <= n + 1; j ++) a [k] [j]-= f * a [I] [j]; 44} 45 debug (); 46} 47 debug (); 48 for (int I = n; I> = 1; I --) 49 {50 debug (); 51 for (int j = I + 1; j <= n; j ++) 52 a [I] [n + 1]-= a [j] [n + 1] * a [I] [j]; 53 a [I] [n + 1]/= a [I] [I]; 54} 55 for (int I = 1; I <= n; I ++) 56 printf ("%. 2lf \ n ", a [I] [n + 1]); 57} 58 int main () 59 {60 read (n); 61 for (int I = 1; I <= n; I ++) 62 for (int j = 1; j <= n + 1; j ++) 63 scanf ("% lf ", & a [I] [j]); 64 gauss_elimination (n); 65 return 0; 66}