Pagini recente » Cod sursa (job #239626) | Cod sursa (job #544075) | Cod sursa (job #431291) | Cod sursa (job #1001268) | Cod sursa (job #2402644)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("gauss.in");
ofstream fout("gauss.out");
double matrix[305][305];
#define verif 0.00000001
int n,m;
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[l][j]>verif || matrix[l][j]<-verif)
break;
if(l==n+1)
{
j++;
continue;
}
if(l!=i)
{
for(c=1;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(8)<<fixed;
for(int i=1;i<=m;i++)
fout<<ans[i]<<" ";
return 0;
}