Pagini recente » Cod sursa (job #3203826) | Cod sursa (job #2961254) | Cod sursa (job #1376428) | Cod sursa (job #2476065)
#include <bits/stdc++.h>
#define eps 1e-6
using namespace std;
double mat[310][310];
double sol[310];
bool eqzero(double x)
{
return abs(x) < eps;
}
int main() {
freopen("gauss.in", "r", stdin);
freopen("gauss.out", "w", stdout);
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= m; j++)
{
cin >> mat[i][j];
}
}
int lc = 0, cc = 0, i, j;
while(lc < n && cc < m)
{
for(i = lc; i < n; i++)
{
if(!eqzero(mat[i][cc]))
break;
}
if(lc == n)
{
cc++;
continue;
}
if(lc != i)
{
for(j = cc; j <= m; j++)
{
swap(mat[lc][j], mat[i][j]);
}
}
for(i = cc + 1; i <= m; i++)
{
mat[lc][i] /= mat[lc][cc];
}
mat[lc][cc] = 1;
for(i = lc + 1; i < n; i++)
{
for(j = cc + 1; j <= m; j++)
{
mat[i][j] -= mat[i][cc] * mat[lc][j];
}
mat[i][cc] = 0;
}
lc++;
cc++;
}
for(i = n - 1; i >= 0; i--)
{
for(j = 0; j <= m; j++)
if(!eqzero(mat[i][j]))
break;
if(j == m + 1)
continue;
if(j == m)
{
cout << "Imposibil";
return 0;
}
int pos = j;
double s = 0;
for(j = pos + 1; j < m; j++)
s -= sol[j] * mat[i][j];
s += mat[i][m];
sol[pos] = s / mat[i][pos];
}
for(i = 0; i < m; i++)
{
cout << fixed << setprecision(10) << sol[i] << ' ';
}
return 0;
}