Cod sursa(job #1795125)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 1 noiembrie 2016 23:55:28
Problema Algoritmul lui Gauss Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;

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

const int DIM = 3e2 + 5;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-5;

double sys[DIM][DIM], sol[DIM]; int pos[DIM];

int main( int argc, const char *argv[] ) {
    ios::sync_with_stdio( false );

    int n, m; in >> n >> m;
    for( int i = 1; i <= n; i ++ ) {
        for( int j = 1; j <= m + 1; j ++ ) {
            in >> sys[i][j]; } }

    for( int i = 1; i <= n; i ++ ) {
        for( int j = 1; j <= m + 1 && pos[i] == 0; j ++ ) {
            if( fabs( sys[i][j] ) > EPS ) { pos[i] = j; } }
        if( pos[i] == 0 ) {
            continue; }
        if( pos[i] == m + 1 ) {
            out << "Imposibil" << endl;
            return 0; }
        for( int j = 1; j <= n; j ++ ) {
            if( i == j ) {
                continue; }
            double aux = sys[j][ pos[i] ] /  sys[i][ pos[i] ];
            for( int k = 1; k <= m + 1; k ++ ) {
                sys[j][k] -= sys[i][k] * aux; } } }
    for( int i = 1; i <= n; i ++ ) { if( pos[i] != 0 ) {
        sol[ pos[i] ] = sys[i][m + 1] / sys[i][ pos[i] ]; } }

    for( int i = 1; i <= m; i ++ ) {
        out << setprecision( 10 ) << fixed << sol[i] << " "; }
    out << endl;

    return 0;
}