Pagini recente » Cod sursa (job #3272825) | Cod sursa (job #2753097) | Cod sursa (job #3215779) | Cod sursa (job #480195) | Cod sursa (job #1614855)
#include <iostream>
#include <fstream>
#include <iomanip>
#define EPS 0.0000001
#define MAXN 300
using namespace std;
double a[MAXN + 1][MAXN + 5];
double sol[MAXN + 5];
int main () {
ifstream cin("gauss.in");
ofstream cout("gauss.out");
int n, m;
cin >> n >> m;
for (int i = 1 ; i <= n ; ++i)
for (int j = 1 ; j <= m + 1 ; ++j)
cin >> a[i][j];
int lin, col;
lin = col = 1;
while (lin <= n && col <= m) {
int k = lin;
while (k <= n && a[k][col] >= -EPS && a[k][col] <= EPS)
++k;
if (k > n) {
++col;
}
else {
if (k != lin) {
for (int j = 1 ; j <= m + 1 ; ++j) {
double aux = a[lin][j];
a[lin][j] = a[k][j];
a[k][j] = aux;
}
}
for (int j = col ; j <= m + 1 ; ++j)
a[lin][j] /= a[lin][col];
for (int i = lin + 1 ; lin <= n ; ++lin)
for (int j = col + 1 ; col <= m + 1 ; ++col)
a[i][j] -= a[i][col] * a[lin][j];
}
++lin;
++col;
}
for (int i = n ; i >= 1 ; --i)
for (int j = 1 ; j <= m + 1 ; ++j)
if (a[i][j] < -EPS || a[i][j] > EPS) {
if (j == m + 1) {
cout << "Imposibil\n";
return 0;
}
sol[j] = a[i][m + 1];
for (int k = j + 1 ; k <= m ; ++k)
sol[j] -= sol[k] * a[i][k];
break;
}
cout << fixed;
for (int i = 1 ; i <= m ; ++i)
cout << setprecision(10) << sol[i] << " ";
return 0;
}