Cod sursa(job #2222648)

Utilizator ContDeRacistAliniateEBlat ContDeRacist Data 17 iulie 2018 16:49:12
Problema Algoritmul lui Gauss Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.92 kb
#include <fstream>
#include <iomanip>
#include <vector>

using namespace std;

ifstream cin("gauss.in");
ofstream cout("gauss.out");

#define is_null(x) (x <= 1e-5 && x >= -1e-5)

bool OK (false);

class Matr {

    public :

        void rem_element(vector < double > & a, vector < double > & b, int c) {
            const double x = b[c] / a[c];
            for (int i = 0; i < a.size(); ++i) {
                b[i] -= a[i] * x;
            }
        }
        vector < vector < double > > el;

        unsigned int n = 0, m = 0;

        Matr (unsigned int _m = 0) {
            m = _m;
        }

        void add_row(vector < double > & x) {
            for (auto & a : el) {

                int poz(0);

                for (int p = 1; p <= m; ++p) {
                    if (is_null(a[p])) {
                        continue;
                    }

                    poz = p;
                    break;
                }

                rem_element(a, x, poz);
            }

            bool useless(true);

            for (int i = 1; i <= m; ++i) {
                if (is_null(x[i])) {
                    continue;
                }

                useless = false;
            }

            if (!useless) {
                el.push_back(x);
                ++n;
            }

            else {
                if (!is_null(x[0])) {
                    cout << "Imposibil";
                    OK = true;
                }
            }
        }

        vector < double > solve() {

            vector < double > sol(m, 0);

            for (int i = n - 1; i >= 0; --i) {
                int to_find(0);

                for (int p = 1; p <= m; ++p) {

                    if (is_null(el[i][p])) {
                        continue;
                    }

                    if (to_find == 0) {
                        to_find = p;
                    }
                    else {
                        el[i][0] -= sol[p - 1] * el[i][p];
                    }

                }

                sol[to_find - 1] = el[i][0] / el[i][to_find];
            }

            return sol;

        }

        void debug_window() {
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j <= m; ++j) {
                    cout << el[i][j] << ' ';
                }
                cout << '\n';
            }

        }

};

int main()
{
    unsigned int n, m;
    cin >> n >> m;
    Matr x(m);
    for (int i = 0; i < n; ++i) {
        vector < double > v(m + 1, 0);
        for (int j = 1; j <= m; ++j) {
            cin >> v[j];
        }
        cin >> v[0];
        x.add_row(v);
        if (OK) {
            return 0;
        }
    }
    vector < double > ans;
    ans = x.solve();
    cout << fixed << setprecision(10);
    for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i] << ' ';
    }
    return 0;
}