Cod sursa(job #2030883)

Utilizator AlexNiuclaeNiculae Alexandru Vlad AlexNiuclae Data 2 octombrie 2017 14:07:25
Problema Algoritmul lui Gauss Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.7 kb
#include <bits/stdc++.h>

using namespace std;

struct psychotronic_induction {
    int electromagnetic_wave = 7;
};

const int inf = 0x3f3f3f3f;
const long long infL = LLONG_MAX;

const int nmax = 300 + 10;
const int mmax = 300 + 10;

const double eps = 1e-10;

int n, m;
double a[nmax][mmax], ans[mmax];
int col[nmax];

void input() {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m + 1; ++j)
            cin >> a[i][j];
}

bool compare(double a, double b, int res) {
    if (a + eps < b) return (-1 == res);
    if (b + eps < a) return (1 == res);
    return (0 == res);
}

void gauss() {
    for (int i = 1; i <= n; ++i) {
        col[i] = 0;
        for (int j = 1; j <= m + 1 && col[i] == 0; ++j)
            if (compare(a[i][j], 0.0, 1) || compare(a[i][j], 0.0, -1)) col[i] = j;

        if (col[i] == m + 1) {
            cout << "Imposibil" << '\n';
            exit(0);
        }

        if (col[i] == 0) continue;

        for (int ii = 1; ii <= n; ++ii) {
            if (ii == i || compare(a[ii][col[i]], 0.0, 0)) continue;

            double r = a[ii][col[i]] / a[i][col[i]];
            for (int j = 1; j <= m + 1; ++j)
                a[ii][j] = a[ii][j] - a[i][j] * r;
        }
    }

    for (int i = 1; i <= n; ++i)
        if (col[i]) ans[col[i]] = a[i][m+1] / a[i][col[i]];
}

void output() {
    cout << fixed << setprecision(10);
    for (int i = 1; i <= m; ++i)
        cout << ans[i] << ' ';
    cout << '\n';
}

int main()
{
    freopen("gauss.in","r",stdin);
    freopen("gauss.out","w",stdout);

    ios_base :: sync_with_stdio(false);

    input();
    gauss();
    output();

    return 0;
}