Pagini recente » Cod sursa (job #3161568) | Cod sursa (job #348693) | Cod sursa (job #2168354) | Cod sursa (job #664062) | Cod sursa (job #2276828)
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#define EPS 0.0000000001
void read(int& n, int&m, double a[305][305]){
std::cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m + 1; ++j)
std::cin >> a[i][j];
}
}
void divideRow(int row, double coef, double a[305][305], int m) {
for (int j = 1; j <= m + 1; ++j) {
a[row][j] /= coef;
}
}
void subtractRow(int row, int from, double coef, double a[305][305], int m) {
for (int j = 1; j <= m + 1; ++j) {
a[row][j] -= coef * a[from][j];
}
}
int main() {
freopen("../gauss.in", "r", stdin);
freopen("../gauss.out", "w", stdout);
int n, m;
double a[305][305];
double sol[305];
read(n, m, a);
for (int row = 1; row <= n; ++row) {
if (std::abs(a[row][row]) < EPS)
break;
divideRow(row, a[row][row], a, m);
for (int i = row + 1; i <= n; ++i)
subtractRow(i, row, a[i][row], a, m);
}
//for (int i = 1; i <= n; ++i) {
// for (int j = 1; j <= m + 1; ++j)
// std::cout << a[i][j] << " ";
// std::cout << std::endl;
//}
sol[n] = a[n][m + 1];
for (int row = n - 1; row >= 1; --row) {
sol[row] = 0;
bool allZero = true;
for (int j = 1; j <= m && allZero; ++j)
if (std::abs(a[row][j]) > EPS)
allZero = false;
if (allZero && std::abs(a[row][m + 1]) > EPS) {
std::cout << "Imposibil";
return 0;
}
for (int j = row + 1; j <= m; ++j) {
sol[row] -= a[row][j] * sol[j];
}
sol[row] += a[row][m + 1];
}
for (int i = 1; i <= m; ++i)
std::cout << std::setprecision(10) << sol[i] << " ";
return 0;
}