Pagini recente » Cod sursa (job #743594) | Cod sursa (job #3147774) | Cod sursa (job #160) | Cod sursa (job #1327688) | Cod sursa (job #1913239)
#include <cstdio>
#include <algorithm>
#define eps 1e-8
using namespace std;
int n, m;
double mat[301][301];
double rez[301];
inline bool ezero(const double& d)
{
return -eps <= d && d <= eps;
}
int main()
{
freopen("gauss.in", "r", stdin);
freopen("gauss.out", "w", stdout);
scanf("%d%d", &n, &m);
int i, j, k, lc, cc;
for(i = 0; i < n; i++)
{
for(j = 0; j <= m; j++)
{
scanf("%lf", &mat[i][j]);
}
}
lc = 0;
cc = 0;
while(lc < n && cc < m)
{
for(k = lc; ezero(mat[k][cc]) && k < n; k++);
if(k == n)
{
cc++;
continue;
}
if(k != lc)
{
for(i = 0; i <= m; i++)
swap(mat[lc][i], mat[k][i]);
}
for(i = cc + 1; i <= m; i++)
mat[lc][i] /= mat[lc][cc];
mat[lc][cc] = 1;
for(i = lc + 1; i < n; i++)
{
if(!ezero(mat[i][cc]))
{
for(j = cc + 1; j <= m; j++)
{
mat[i][j] /= mat[i][cc];
mat[i][j] -= mat[lc][j];
}
mat[i][cc] = 0;
}
}
lc++; cc++;
}
for(i = n - 1; i >= 0; i--)
{
for(j = 0; j < m && ezero(mat[i][j]); j++);
if(j == m && !ezero(mat[i][j]))
{
printf("Imposibil");
return 0;
}
double st = 0;
for(k = j + 1; k < m; k++)
st += rez[k] * mat[i][k];
rez[j] = mat[i][m] - st;
}
for(i = 0; i < m; i++)
printf("%.10lf ", rez[i]);
return 0;
}