Pagini recente » Cod sursa (job #2774146) | Cod sursa (job #1720441) | Cod sursa (job #1235597) | Cod sursa (job #1236520) | Cod sursa (job #2749819)
#include <bits/stdc++.h>
#define llg long long
#define double long double
template <typename type>
type modulus(type a) { return std::max(a, -a); }
class GaussCalculator {
public:
static int run(std::vector <std::vector<double>> a, std::vector <double> &ans) {
const double EPS = 1e-9;
int n = (int) a.size();
int m = (int) a[0].size() - 1;
std::vector <int> where(m, -1);
for (int col=0, row=0; col<m && row<n; ++col) {
int sel = row;
for (int i=row; i<n; ++i)
if (modulus(a[i][col]) > modulus(a[sel][col]))
sel = i;
if (modulus(a[sel][col]) < EPS)
continue;
for (int i=col; i<=m; ++i)
std::swap(a[sel][i], a[row][i]);
where[col] = row;
for (int i=0; i<n; ++i)
if (i != row) {
double c = a[i][col] / a[row][col];
for (int j=col; j<=m; ++j)
a[i][j] -= a[row][j] * c;
}
++ row;
}
ans.assign(m, 0);
for (int i=0; i<m; ++i)
if (where[i] != -1)
ans[i] = a[where[i]][m] / a[where[i]][i];
for (int i=0; i<n; ++i) {
double sum = 0;
for (int j=0; j<m; ++j)
sum += ans[j] * a[i][j];
if (modulus(sum - a[i][m]) > EPS)
return 0;
}
for (int i=0; i<m; ++i)
if (where[i] == -1)
return 2;
return 1;
}
};
int32_t main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::ifstream input ("gauss.in");
std::ofstream output("gauss.out");
int N, M; input >> N >> M;
std::vector <std::vector <double>> a(N);
for (auto &it:a) {
it.resize(M+1);
for (auto &x:it) input >> x;
}
std::vector <double> ans;
auto ret = GaussCalculator::run(a, ans);
if (ret == 0) output << "Imposibil\n";
else {
for (auto &x : ans) output << std::fixed << std::setprecision(8) << x << ' ';
}
return 0;
}