include iostream include vector include iomanip include cmath using na

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
int n;
cin >> n;
vector <vector <double> > matrix (n, vector<double> (n + 1));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n + 1; j++) {
cin >> matrix[i][j];
}
}
double eps;
cin >> eps;
vector <double> pre_values (n, 0.0);
while (true) {
vector <double> cur_values (n);
for (int i = 0; i < n; i++) {
cur_values[i] = matrix[i][n];
for (int j = 0; j < n; j++) {
if (i != j) {
cur_values[i] -= matrix[i][j] * pre_values[j];
}
}
cur_values[i] /= matrix[i][i];
}
double error = 0.0;
for (int i = 0; i < n; i++) {
error += abs (cur_values[i] - pre_values[i]);
}
if (error < eps) {
break;
}
pre_values = cur_values;
}
cout <<fixed <<setprecision(5);
for (int i = 0; i < n; i++) {
cout <<"X" <<i + 1 <<" = " <<pre_values[i] <<endl;
}
return 0;
}