Pagini recente » Cod sursa (job #1338244) | Cod sursa (job #1675838) | Cod sursa (job #1778310) | Cod sursa (job #1780998) | Cod sursa (job #1199596)
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
const double eps = 1e-8;
bool iszero(double x)
{
return abs(x) < eps;
}
int main()
{
ios::sync_with_stdio(false);
freopen("gauss.in", "r", stdin);
freopen("gauss.out", "w", stdout);
int n, m;
cin >> n >> m;
vector<vector<double>> a(n, vector<double>(m+1));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m+1; j++) {
cin >> a[i][j];
}
}
vector<int> head(n, m);
for (int i = 0; i < n; i++) {
if (i == 0) {
head[i] = 0;
}
else {
head[i] = head[i-1]+1;
}
while (head[i] < m) {
for (int j = i; j < n; j++) {
if (!iszero(a[j][head[i]])) {
swap(a[i], a[j]);
break;
}
}
if (!iszero(a[i][head[i]])) {
break;
}
head[i]++;
}
if (head[i] == m) {
break;
}
double x = a[i][head[i]];
for (int j = head[i]; j < m+1; j++) {
a[i][j] /= x;
}
for (int j = i+1; j < n; j++) {
double x = a[j][head[i]];
for (int k = head[i]; k < m+1; k++) {
a[j][k] -= x*a[i][k];
}
}
}
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m+1; j++) {
// printf("%f ", a[i][j]);
// }
// puts("");
// }
// for (int i = 0; i < n; i++) {
// printf("%d ", head[i]);
// }
// puts("");
// return 0;
vector<double> ans(m);
for (int i = n-1; i >= 0; i--) {
if (head[i] == m) {
if (!iszero(a[i][m])) {
puts("Imposibil");
return 0;
}
continue;
}
double x = a[i][m];
for (int j = head[i]+1; j < m; j++) {
x -= a[i][j]*ans[j];
}
ans[head[i]] = x;
}
for (auto x: ans) {
printf("%.10f ", x);
}
puts("");
return 0;
}