Pagini recente » Cod sursa (job #2832691) | Cod sursa (job #2330249) | Cod sursa (job #3285737) | Cod sursa (job #3283824) | Cod sursa (job #3225448)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("gauss.in");
ofstream fout("gauss.out");
typedef long double ld;
int n,m;
ld v[305][305];
bool use[305];
ld sol[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(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 val=v[i][j];
for(int k=1;k<=m+1;k++)
v[i][k]/=val;
for(int u=i+1;u<=n;u++)
{
val=v[u][j];
for(int k=1;k<=m+1;k++)
v[u][k]-=val*v[i][k];
}
i++;
j++;
}
for(int i=n;i>=1;i--)
{
ld suma=0;
int who=0;
for(int j=1;j<=m;j++)
if(v[i][j]!=0)
{
if(use[j])
{
suma+=v[i][j]*sol[j];
continue;
}
if(who==0)
{
who=j;
continue;
}
sol[who]=0;
use[who]=1;
who=j;
}
if(who==0)
{
if(suma!=v[i][m+1])
{
fout<<"Imposibil";
return 0;
}
continue;
}
use[who]=1;
ld val=(v[i][m+1]-suma)/v[i][who];
sol[who]=val;
}
for(int i=1;i<=m;i++)
fout<<fixed<<setprecision(10)<<sol[i]<<' ';
return 0;
}