Pagini recente » Cod sursa (job #383783) | Cod sursa (job #2406675) | Cod sursa (job #636136) | Istoria paginii preoni-2008/clasament/runda-3/9 | Cod sursa (job #1370891)
#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; j ++)
if (a [i][j] > eps || a [i][j] <= -eps) {
if (ans [j] > eps || ans [j] <= -eps)
s = s + ans [j] * a [i][j];
else
x = j;
}
if (x == -1) {
printf ("Imposibil\n");
return 0;
}
ans [x] = (a [i][m + 1] - s) / a [i][x];
}
for (i = 1; i <= m; i ++)
printf ("%lf ", ans [i]);
printf ("\n");
return 0;
}