Cod sursa(job #2402624)

Utilizator PredaBossPreda Andrei PredaBoss Data 10 aprilie 2019 20:58:23
Problema Algoritmul lui Gauss Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.53 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("gauss.in");
ofstream fout("gauss.out");
long double matrix[305][305];
const long double verif=0.000000001;
int n,m;
long double ans[305];
int main()
{
    fin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m+1;j++)
            fin>>matrix[i][j];
    int i=1;
    int j=1;
    while(i<=n && j<=m)
    {
        int l,c;
        for(l=i;l<=n;j++)
            if(matrix[i][j]>verif || matrix[i][j]<-verif)
                break;
        if(l==n+1)
        {
            j++;
            break;
        }
        if(l!=i)
        {
            for(c=j;c<=m+1;c++)
                swap(matrix[l][c],matrix[i][c]);
        }
        for(c=j+1;c<=m+1;c++)
            matrix[i][c]/=matrix[i][j];
        matrix[i][j]=1;
        for(l=i+1;l<=n;l++)
        {
            for(c=j+1;c<=m+1;c++)
                matrix[l][c]-=matrix[l][j]*matrix[i][c];
            matrix[l][j]=0;
        }
        i++;
        j++;
    }
    for(i=n;i>=1;i--)
        for(j=1;j<=m+1;j++)
            if(matrix[i][j]>verif || matrix[i][j]<-verif)
            {
                if(j==m+1)
                {
                    fout<<"Imposibil";
                    return 0;
                }
                ans[j]=matrix[i][m+1];
                for(int g=j+1;g<=m;g++)
                    ans[j]-=matrix[i][g]*ans[g];
                break;
            }
    fout<<setprecision(10)<<fixed;
    for(int i=1;i<=m;i++)
        fout<<ans[i]<<" ";
    return 0;
}