Cod sursa(job #1672498)

Utilizator usermeBogdan Cretu userme Data 2 aprilie 2016 19:59:36
Problema Algoritmul lui Gauss Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include <fstream>
#include <iomanip>

using namespace std;

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

double ec[305][305];

int n, m;

void swapL(int a, int b) {
    if (a == b) {
        for (int i = 1; i <= m + 1; ++i) {
            double aux = ec[a][i];
            ec[a][i] = ec[b][i];
            ec[a][i] = aux;
        }
    }
}

int main() {
    cin>>n>>m;
    if (n < m) {
        cout<<"Imposibil\n";
        return 0;
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m + 1; ++j) {
            cin>>ec[i][j];
        }
    }
    for (int i = 1; i <= m; ++i) {
        int rez = -1;
        for (int j = 1; j <= n; ++j) {
            if (ec[j][i] != 0) {
                rez = j;
                break;
            }
        }
        if (rez == -1) {
            cout<<"Imposibil\n";
            return 0;
        }
        swapL(i, rez);
        double factor = ec[i][i];
        for (int j = 1; j <= m + 1; ++j) {
            ec[i][j] /= factor;
        }
        for (int j = 1; j <= n; ++j) {
            if (i != j) {
                double factor = ec[j][i] / ec[i][i];
                for (int k = 1; k <= m + 1; ++k) {
                    ec[j][k] -= ec[i][k] * factor;
                }
            }
        }
    }
    bool corect = 1;
    for (int i = m + 1; i <= n; ++i) {
        if (ec[i][m + 1]) {
            corect = 0;
        }
    }
    if (corect = 0) {
        cout<<"Imposibil\n";
    }
    for (int i = 1; i <= m; ++i) {
        cout<<setprecision(10)<<ec[i][m + 1]<<" ";
    }
    cout<<"\n";
    return 0;
}