Pagini recente » Cod sursa (job #808860) | Cod sursa (job #955918) | Cod sursa (job #2520528) | Cod sursa (job #1326181) | Cod sursa (job #2786591)
#include <bits/stdc++.h>
using namespace std;
ifstream f("gauss.in");
ofstream g("gauss.out");
#define cin f
#define cout g
#define int long long
#define double long double
const int Max = 1e5 + 1;
void nos()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const double INF = 1e10;
const double EPS = 1e-10;
//http://e-maxx.ru/algo/linear_systems_gauss
int GaussianElimination (vector < vector<double> > a, vector<double> & ans) {
int n = (int) a.size();
int m = (int) a[0].size() - 1;
vector<int> where (m, -1);
for (int col=0, row=0; col<m && row<n; ++col) {
int sel = row;
for (int i=row; i<n; ++i)
if (abs (a[i][col]) > abs (a[sel][col]))
sel = i;
if (abs (a[sel][col]) < EPS)
continue;
for (int i=col; i<=m; ++i)
swap (a[sel][i], a[row][i]);
where[col] = row;
for (int i=0; i<n; ++i)
if (i != row) {
double c = a[i][col] / a[row][col];
for (int j=col; j<=m; ++j)
a[i][j] -= a[row][j] * c;
}
++row;
}
ans.assign (m, 0);
for (int i=0; i<m; ++i)
if (where[i] != -1)
ans[i] = a[where[i]][m] / a[where[i]][i];
for (int i=0; i<n; ++i) {
double sum = 0;
for (int j=0; j<m; ++j)
sum += ans[j] * a[i][j];
if (abs (sum - a[i][m]) > EPS)
return 0;
}
for (int i=0; i<m; ++i)
if (where[i] == -1)
return INF;
return 1;
}
int n,m;
vector < vector < double > > mat;
void read()
{
f>>n>>m;
mat.resize(n);
int i,j;
for(i=0;i<n;i++)
{
mat[i].resize(m + 1);
for(j=0;j<=m;++j)
f>>mat[i][j];
}
}
void solve()
{
int i,j;
vector < double > sol;
int tp = GaussianElimination(mat,sol);
if(tp == 0)
{
cout<<"Imposibil";
return;
}
for(auto it : sol)
cout<<fixed<<setprecision(10)<<it<<' ';
}
void restart()
{
}
int32_t main()
{
nos();
read();
solve();
restart();
return 0;
}