Pagini recente » Diferente pentru utilizator/stecy intre reviziile 1 si 2 | Diferente pentru utilizator/stecy intre reviziile 5 si 4 | Diferente pentru utilizator/stecy intre reviziile 5 si 3 | Diferente pentru utilizator/stecy intre reviziile 3 si 4 | Cod sursa (job #1696886)
#include <bits/stdc++.h>
#define maxN 302
#define e 0.0000000001
using namespace std;
int n, m;
double a[maxN][maxN], ans[maxN];
void Swap(double &x, double &y)
{
double aux;
aux = x;
x = y;
y = aux;
}
void read()
{
int i, j;
freopen("gauss.in", "r", stdin);
scanf("%d %d", &n, &m);
for (i = 1; i <= n; ++ i)
for (j = 1; j <= m + 1; ++ j)
scanf("%lf", &a[i][j]);
}
void Gauss()
{
int i = 1, j = 1;
while (i <= n && j <= m)
{
int x, y;
for (x = i; x <= n; ++ x)
if (a[x][j] > e || a[x][j] < -e)
break;
if (x == n + 1)
{
++ j;
continue;
}
if (x != i)
for (y = 1; y <= m + 1; ++ y)
Swap(a[x][y], a[i][y]);
for (y = j + 1; y <= m + 1; ++ y)
a[i][y] = (double)(a[i][y] / a[i][j]);
a[i][j] = 1.00000000000;
for (y = i + 1; y <= n; ++ y)
{
int c;
for (c = j + 1; c <= m + 1; ++ c)
a[y][c] -= a[y][j] * a[i][c];
a[y][j] = 0.0000000000;
}
++ i;
++ j;
}
}
void Coef()
{
int i, j, x;
for (i = n; i >= 1; -- i)
for (j = 1; j <= m + 1; ++ j)
if (a[i][j] > e || a[i][j] < -e)
{
if (j == m + 1)
{
ans[0] = -1;
return ;
}
ans[j] = a[i][m + 1];
for (x = j + 1; x <= m; ++ x)
ans[j] -= a[i][x] * ans[x];
break;
}
}
void solve()
{
Gauss();
Coef();
}
void write()
{
int i;
freopen("gauss.out", "w", stdout);
if (ans[0] == -1)
printf("Imposibil\n");
else
for (i = 1; i <= m; ++ i)
printf("%.10lf ", ans[i]);
}
int main()
{
read();
solve();
write();
return 0;
}