Pagini recente » Cod sursa (job #2173683) | Cod sursa (job #2204636) | Cod sursa (job #1957233) | Cod sursa (job #2822174) | Cod sursa (job #3230919)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("gauss.in");
ofstream fout("gauss.out");
typedef long double ld;
const ld EPS=1e-15;
int n,m;
ld v[305][305];
ld sol[305];
bool use[305];
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(0);
fin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m+1;j++)
fin>>v[i][j];
int i=1;
int j=1;
while(i<=n&&j<=m)
{
int lin=0;
for(int l=i;l<=n;l++)
if(v[l][j]!=0)
{
lin=l;
break;
}
if(lin==0)
{
j++;
continue;
}
swap(v[i],v[lin]);
ld z=v[i][j];
for(int p=1;p<=m+1;p++)
v[i][p]/=z;
for(int l=i+1;l<=n;l++)
{
ld u=v[l][j];
for(int p=1;p<=m+1;p++)
v[l][p]-=u*v[i][p];
}
i++;
j++;
}
for(int i=n;i>=1;i--)
{
int who=0;
ld suma=0;
for(int j=1;j<=m;j++)
if(v[i][j]!=0)
{
if(use[j])
{
suma+=sol[j]*v[i][j];
continue;
}
use[who]=1;
sol[who]=0;
who=j;
}
if(who==0)
{
if(abs(suma-v[i][m+1])>EPS)
{
fout<<"Imposibil";
return 0;
}
}
ld val=(v[i][m+1]-suma)/v[i][who];
use[who]=1;
sol[who]=val;
}
for(int i=1;i<=m;i++)
fout<<fixed<<setprecision(10)<<sol[i]<<' ';
return 0;
}