Cod sursa(job #1752434)

Utilizator ZenusTudor Costin Razvan Zenus Data 3 septembrie 2016 21:34:40
Problema Algoritmul lui Gauss Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>

using namespace std;

const int nmax = 309;
const double eps = 0.000000001;

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

bool zero(double x)
{
    if (-eps < x && x < eps) return true;
    return false;
}

int main()
{

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

fin >> m >> n;

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

for (i = 1 ; i <= m ; ++i)
{
    for (j = 1 ; j <= n + 1 && zero(a[i][j]) ; ++j);
    if (j == n + 2) continue;
    if (j == n + 1)
    {
        cout << "Imposibil\n";
        return 0;
    }

    p[i] = j;

    x = a[i][p[i]];
    for (j = 1 ; j <= n + 1 ; ++j)
    a[i][j] /= x;

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

        x = a[j][p[i]];
        if (zero(x)) continue;

        for (k = 1 ; k <= n + 1 ; ++k)
        a[j][k] /= x;

        for (k = 1 ; k <= n + 1 ; ++k)
        a[j][k] -= a[i][k];
    }
}

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

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

return 0;
}