Cod sursa(job #2078319)

Utilizator ListenerRavasz Tamas Listener Data 29 noiembrie 2017 12:20:52
Problema Algoritmul lui Gauss Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 4.08 kb
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

double EPS = 1e-10;

const int nm = 300;

double er[nm+1];
double a[nm+1][nm+1];
int n, m;
ifstream fin("gauss.in");
ofstream fout("gauss.out");

bool megoldhato()
{
    for(int i = n; i >= 1; --i)
    {
        for(int j = 0 ; j <= m + 1; ++j)
        {
            if(abs(a[i][j]) > EPS)
            {
                if(j == m+1)
                    return 0;
                er[j] = a[i][m+1];
                for(int k = j + 1; k <= m; ++k)
                {
                    er[j] -= er[k] * a[i][k];
                }
                /*for(int i = 1; i <=m; ++i)
                    cout << er[i];
                cout << endl;*/
                break;
            }
        }
    }

   /* for(int i = 1; i <=m; ++i)
        cout << er[i];
    cout << endl;*/
    return 1;
}

int main()
{
   /* int i;
    int a = 2;


    for(int j = 0; j < 10; ++j)
    {
        for(int k = 0 ; k < 10; ++k)
        {

            cout << k;
            if(k == 2)
                break;

        }

        cout << endl;
        cout << j << endl;
    }
    cout << endl << endl;
    for(i = 0 ; i < 10; ++i)
    {
        for(int j = 0; j < 5; ++j)
            cout << j;

        if(i==2)continue;
            cout << i;

        cout << i;

        cout << 5;
        cout << endl;
    }
    */

    fin >> n >> m;
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= m + 1; ++j)
        {
            fin >> a[i][j];
        }
    }
    int k,i=1,j=1;

    while(i <= n, j <= m)
    {
        bool ok = 0;
        for(k = i; k <=n ; ++k)
        {
            //cout << a[k][j] << endl;
            if(abs(a[k][j]) > EPS)
            {
                ok = 1;
                for(int l = j; l <= m + 1; ++l)
                {
                    swap(a[i][l], a[k][l]);
                    /*for(int z = 1 ; z <= n; ++z)
                        {
                            for(int c = 1 ; c <= m + 1; ++c)
                            {
                                cout << setw(10) << a[z][c] << " ";
                            }
                            cout << endl;
                        }
                        getchar();*/
                }
                break;
            }
            else
                a[k][j] = 0;
        }

        if(ok == 0)
        {
            ++j;
            continue;
        }


        for(int l = j + 1; l <= m+1; ++l)
        {
            a[i][l] = a[i][l]/a[i][j];
        }

        /*for(int z = 1 ; z <= n; ++z)
        {
            for(int c = 1 ; c <= m + 1; ++c)
            {
                cout << setw(10) << a[z][c] << " ";
            }
            cout << endl;
        }
        getchar();*/

        a[i][j] = 1;

        /*for(int z = 1 ; z <= n; ++z)
        {
            for(int c = 1 ; c <= m + 1; ++c)
            {
                cout << setw(10) << a[z][c] << " ";
            }
            cout << endl;
        }
        getchar();*/

        /*for(int z = 1 ; z <= n; ++z)
        {
            for(int c = 1 ; c <= m + 1; ++c)
            {
                cout << setw(10) << a[z][c] << " ";
            }
            cout << endl;
        }
        getchar();*/

        for(int u = i + 1;  u <= n; ++u)
        {
            for(int l = j + 1; l <= m+1; ++l)
            {
                a[u][l] = a[u][l] - a[u][j] * a[i][l];

            }
            a[u][j] = 0;
        }


        ++i;
        ++j;
    }

    /*for(int z = 1 ; z <= n; ++z)
        {
            for(int c = 1 ; c <= m + 1; ++c)
            {
                cout << setw(10) << a[z][c] << " ";
            }
            cout << endl;
        }
        getchar();*/

    if(megoldhato())
        {
            for(int i = 0 ; i <= m; ++i)
            {
                fout << fixed << setprecision(10) << er[i] << " ";
            }
        }
    else
        fout << "Imposibil";
    return 0;
}