include iostream include vector include cmath using namespace std int

 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
int n;
cin >> n;
vector <vector<double> > a(n, vector <double> (n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >>a[i][j];
}
}
vector <double> b(n);
for (int i = 0; i < n; ++i) {
cin >>b[i];
}
cout << "n:" << n << endl;
cout << "matr a:" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cout << a[i][j] <<" ";
cout << endl;
}
cout << "vect b:" << endl;
for (int i; i < n; i++) {
cout << b[i] << endl;
}
cout << endl;
//сам метод
vector <vector<double> > t(n, vector <double> (n - 1));
int k;
for (k = 0; k < n; k++) {
cout << "k:" << k << endl;
double max = a[k][k];
int indexMax = k;
for (int i = k + 1; i < n; i++) {
if (a[i][k] > max) {
max = a[i][k];
indexMax = i;
}
}
cout << "max " << max << endl;
if (abs(max) < 1e-8) {
cout << "Однозначного решения нету" << endl;
return 0;
} else {
swap (b[k], b[indexMax]);
for (int j = k; j < n; j++) {
swap (a[k][j], a[indexMax][j]);
}
}
for (int i = k + 1; i < n; i++) {
t[i][k] = a[i][k] / a[k][k];
b[i] = b[i] - t[i][k] * b[k];
for (int j = k + 1; j < n; j++) {
a[i][j] = a[i][j] - t[i][k] * a[k][j];
}
}
cout << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout <<a[i][j] <<' ';
}
cout <<endl;
}
for (int i = 0; i < n; ++i) {
cout <<b[i] <<' ';
}
cout <<endl;
}
vector <double> answers(n);
answers[n - 1] = b[n -1]/a[n - 1][n - 1];
for (k = n - 2; k >= 0; k--) {
double sum = 0.0;
for (int j = k + 1; j <= n; j++) {
sum += a[k][j] * answers[j];
}
answers[k] = (b[k] - sum) / a[k][k];
}
for (int i = 0; i < n; i++) {
cout << answers[i] << endl;
}
}