Pagini recente » Cod sursa (job #3195331) | Cod sursa (job #51820) | Cod sursa (job #1243715) | Cod sursa (job #307581) | Cod sursa (job #1370936)
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 307;
const double eps = 1.e-14;
double a [N][N], aux [N], ans [N];
int main () {
int n, m, i, j, k, h, x;
double s, z;
freopen ("gauss.in", "r", stdin);
freopen ("gauss.out", "w", stdout);
scanf ("%d%d", &n, &m);
for (i = 1; i <= n; i ++)
for (j = 1; j <= m + 1; j ++)
scanf ("%lf", &a [i][j]);
i = j = 1;
while (i <= n && j <= m) {
for (x = i; x <= n; x ++)
if (a [x][j] < -eps || a [x][j] > eps)
break;
if (fabs (a [x][j]) < eps) {
j ++;
continue;
}
if (i != x) {
memcpy (aux, a [i], sizeof (a [i]));
memcpy (a [i], a [x], sizeof (a [x]));
memcpy (a [x], aux, sizeof (aux));
}
z = a [i][j];
for (h = j; h <= m + 1; h ++)
a [i][h] = a [i][h] / z;
for (k = i + 1; k <= n; k ++) {
z = a [k][j];
for (h = j; h <= m + 1; h ++)
a [k][h] -= z * a [i][h];
}
++ i; ++ j;
}
for (i = n; i >= 1; i --) {
x = -1;
s = 0;
for (j = 1; j <= m + 1; j ++) {
if (a [i][j] > eps || a [i][j] < -eps) {
if (j == m + 1) {
printf ("Imposibil\n");
return 0;
}
s = 0;
for (h = j + 1; h <= m; h ++)
s = s + ans [h] * a [i][h];
ans [j] = a [i][m + 1] - s;
break;
}
}
}
for (i = 1; i <= m; i ++)
printf ("%lf ", ans [i]);
printf ("\n");
return 0;
}