Pagini recente » Cod sursa (job #1223686) | Cod sursa (job #2707134) | Cod sursa (job #553835) | Cod sursa (job #2384097) | Cod sursa (job #1961599)
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream fin("gauss.in");
ofstream fout("gauss.out");
int N,M,row[305],ok=1;
double E[305][305];
const double kEps = 1e-8;
bool Zero(double d)
{
return (d > -kEps) && (d < kEps);
}
void Read()
{
fin>>N>>M;
for(int i=1;i<=N;i++)
for(int j=1;j<=M+1;j++)
fin>>E[i][j];
}
void Solve()
{
int r=1,c=1;
while(r<=N && c<=M)
{
if(Zero(E[r][c]))
{
int i=r+1;
while(i<=N && Zero(E[i][c]))
i++;
if(i>N)
{
c++;
continue;
}
swap(E[r],E[i]);
}
for(int i=1;i<=N;i++)
{
if(i==r)
continue;
double k=E[i][c]/E[r][c];
for(int j=1;j<=M+1;j++)
E[i][j]-=k*E[r][j];
}
row[c++]=r++;
}
if(c>M)
{
for(;r<=N;r++)
{
if(!Zero(E[r][M+1]))
{
fout<<"Imposibil\n";
ok=0;
break;
}
}
}
}
void Print()
{
fout<<setprecision(10)<<fixed;
for (int i=1;i<=M;i++)
fout << (row[i] ? (E[row[i]][M + 1] / E[row[i]][i]) : 0.0) << " ";
fout << "\n";
}
int main()
{
Read();
Solve();
if(ok) Print();
return 0;
}