Pagini recente » Cod sursa (job #2751275) | Cod sursa (job #945969) | Cod sursa (job #670567) | Cod sursa (job #2793948) | Cod sursa (job #1402849)
// How about a coding trick?
#include <cstdio>
#include <algorithm>
#define DIM 330
#define EPS 0.0000000001
using namespace std;
FILE *fin=freopen("gauss.in","r",stdin);
FILE *fout=freopen("gauss.out","w",stdout);
int n, m;
double A[DIM][DIM], Sol[DIM];
void Read()
{
int i, j;
scanf("%d %d ", &n, &m);
for(i = 1; i <= n; ++i)
for(j = 1; j <= m + 1; ++j)
scanf("%lf", &A[i][j]);
}
void Solve()
{
int i, j, l, k;
i = j = 1;
while( i <= n && j <= m )
{
for(l = i; l <= n; ++l)
if( EPS < A[l][j] || -EPS > A[l][j] )
break;
if( l == n + 1 )
{
++j; continue;
}
for(k = 1; k <= m + 1; ++k)
swap( A[i][k], A[l][k] );
for(k = j + 1; k <= m + 1; ++k)
A[i][k] /= A[i][j];
A[i][j] = 1;
for(int p = i + 1; p <= n; ++p)
{
for(int q = j + 1; q <= m + 1; ++q)
A[p][q] -= A[i][q] * A[p][j];
A[p][j] = 0;
}
++i, ++j;
}
for(i = n; i; --i)
for(j = 1; j <= m + 1; ++j)
if( A[i][j] < - EPS || EPS < A[i][j] )
{
if( j == m + 1 )
{
printf("IMPOSIBIL\n");
return;
}
Sol[j] = A[i][m + 1];
for(l = m; l > j; --l)
Sol[j] -= Sol[l] * A[i][l];
break;
}
for(i = 1; i <= m; ++i)
printf("%.10f ", Sol[i]);
}
int main()
{
Read();
Solve();
return 0;
}