Pagini recente » Cod sursa (job #3263462) | Cod sursa (job #3272823) | Cod sursa (job #2342107) | Cod sursa (job #2938722) | Cod sursa (job #1676497)
#include<fstream>
#include<iomanip>
using namespace std;
#define EPS 1e-10
ifstream fin("gauss.in");
ofstream fout("gauss.out");
int n,m;
double A[305][305],X[305];
int main()
{
int i,j,k,aux;
fin>>m>>n;
for (i=1;i<=m;++i)
for (j=1;j<=n+1;++j)
fin>>A[i][j];
i=1, j=1;
while (i<=m && j<=n)
{
for (k=i;k<=m && -EPS<A[k][j] && A[k][j]<EPS;++k);
if (k>m)
{
++j;
continue;
}
if (k>i)
for (int t=1;t<=n+1;++t)
aux=A[i][t], A[i][t]=A[k][t], A[k][t]=aux;
for (int t=j+1;t<=n+1;++t)
A[i][t]/=A[i][j];
A[i][j]=1.0;
for (int u=1;u<=m;++u)
{
if (u==i) continue;
for (int t=j+1;t<=n+1;++t)
A[u][t]-=A[i][t]*A[u][j];
A[u][j]=0.0;
}
++i, ++j;
}
// for (i=1;i<=m;++i)
// {
// for (j=1;j<=n+1;++j)
// fout<<A[i][j]<<" ";
// fout<<"\n";
// }
// fout<<"\n";
for (i=1;i<=m;++i)
{
bool ok=false;
for (j=1;j<=n && !ok;++j)
if (A[i][j]<-EPS || A[i][j]>EPS)
ok=true;
if (!ok && (A[i][j]<-EPS || A[i][j]>EPS))
{
fout<<"Imposibil\n";
return 0;
}
if (ok)
X[i]=A[i][n+1];
}
for (i=1;i<=n;++i)
fout<<setprecision(10)<<fixed<<X[i]<<" ";
fout<<"\n";
return 0;
}