Cod sursa(job #1414120)

Utilizator UVS_Miriam_Piro_DianaFrumoasele si Bestialul UVS_Miriam_Piro_Diana Data 2 aprilie 2015 13:09:31
Problema Algoritmul lui Gauss Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include<fstream>
using namespace std;
ifstream in("gauss.in");
ofstream out("gauss.out");
const double eps = 1e-12;
const int Nmax = 302;
double a[Nmax][Nmax];
double ans[Nmax];
int eq(double a,double b){return (b<=a+eps && a<=b+eps);}
int main(){
    int N,M;
    in>>N>>M;
    for(int i=1;i<=N;i++){
        for(int j=1;j<=M+1;j++){
            in>>a[i][j];
        }
    }
    int s=1,i,j;
    for(int j=1;j<=M;j++){
        for(i=s;i<=N && eq(a[i][j],0);i++);
        if(i<=N){
            for(int k=M+1;k>=j;k--) a[i][k]/=a[i][j];
            for(int ii=i+1;ii<=N;ii++) for(int k=M+1;k>=j;k--) a[ii][k]-=a[ii][j]*a[i][k];
        }
        s=i+1;
    }
    for(i=s;i<=N;i++) if(!eq(a[i][M+1],0)){
        out<<"Imposibil\n";
        return 0;
    }
    for(int i=N;i>=1;i--){
        for(j=1;j<=M && eq(a[i][j],0);j++);
        if(j<=M){
            for(int k=j+1;k<=M;k++) ans[j]+=ans[k]*a[i][k];
            ans[j]=a[i][M+1]-ans[j];
        }
    }
    out.precision(11);
    for(int i=1;i<M;i++) out<<fixed<<ans[i]<<' ';
    out<<ans[M]<<'\n';
    return 0;
}