Cod sursa(job #1689751)

Utilizator ZenusTudor Costin Razvan Zenus Data 14 aprilie 2016 15:46:56
Problema Algoritmul lui Gauss Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std;

const double eps = 1e-8;
const int nmax = 309;

int p[nmax] , i , j , k , n , m , first;
double a[nmax][nmax] , x[nmax] , c;

int main()
{
ifstream fin("gauss.in");
ofstream fout("gauss.out");

fin >> n >> m;

for (i = 1 ; i <= n ; ++i)
for (j = 1 ; j <= m + 1 ; ++j)
fin >> a[i][j];

for (i = 1 ; i <= n ; ++i)
{
    first = 1;
    while (-eps < a[i][first] && a[i][first] < eps) first++;

    if (first == m + 2) continue;
    if (first == m + 1)
    {
        printf("Imposibil\n");
        return 0;
    }

    for (j = 1 ; j <= n ; ++j)
    {
        if (i == j) continue;

        c = a[j][first] / a[i][first];
        for (k = 1 ; k <= m + 1 ; ++k)
        a[j][k] -= a[i][k] * c;
    }

    p[i] = first;
}

for (i = 1 ; i <= m ; ++i)
if (p[i]) x[p[i]] = a[i][m + 1] / a[i][p[i]];

for (i = 1 ; i <= m ; ++i)
fout << fixed << setprecision(10) << x[i] << '\n';

return 0;
}