Pagini recente » Cod sursa (job #2999755) | Cod sursa (job #2929861) | Cod sursa (job #3220406) | Cod sursa (job #2999754) | Cod sursa (job #3220610)
#include <bits/stdc++.h>
#define cin fin
#define cout fout
using namespace std;
ifstream fin("gauss.in");
ofstream fout("gauss.out");
typedef long double ld;
const ld eps = 1e-9;
int gauss(int n, int m, vector<vector<ld>> a, vector<ld> &ans)
{
vector<int> wh(m + 1);
for (int col = 1, row = 1; col <= m; ++col)
{
int qui = row;
for (int i = row; i <= n; ++i)
{
if (abs(a[i][col]) > abs(a[qui][col]))
{
qui = i;
}
}
swap(a[row], a[qui]);
if (abs(a[row][col]) < eps)
{
continue;
}
wh[col] = row;
ld clown = a[row][col];
for (int i = col; i <= m + 1; ++i)
{
a[row][i] /= clown;
}
for (int i = 1; i <= n; ++i)
{
if (i != row)
{
ld clown = a[i][col];
for (int j = col; j <= m + 1; ++j)
{
a[i][j] -= a[row][j] * clown;
}
}
}
++row;
}
ans.resize(m + 1);
for (int i = 1; i <= m; ++i)
{
if (wh[i])
{
ans[i] = a[wh[i]][m + 1];
}
}
for (int i = 1; i <= n; ++i)
{
ld sum = 0;
for (int j = 1; j <= m; ++j)
{
sum += a[i][j] * ans[j];
}
if (abs(sum - a[i][m + 1]) > eps)
{
return 0;
}
}
return 1;
}
int32_t main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<vector<ld>> a(n + 1, vector<ld>(m + 2));
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m + 1; ++j)
{
cin >> a[i][j];
}
}
vector<ld> ans;
if (gauss(n, m, a, ans))
{
for (int i = 1; i <= m; ++i)
{
cout << fixed << setprecision(10) << ans[i] << ' ';
}
}
else
{
cout << "Imposibil\n";
}
}