Pagini recente » Cod sursa (job #2757350) | Cod sursa (job #2636792) | Cod sursa (job #2245797) | Cod sursa (job #2385766) | Cod sursa (job #3287432)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("gauss.in");
ofstream fout("gauss.out");
const double eps = 1e-9;
int n, m;
vector < vector <double> > a;
vector <double> b;
vector <double> sol;
vector <int> poz;
int gauss(){
int r = 0, c = 0;
sol.assign(m, 0);
poz.assign(m, -1);
for(; c < m && r < n; c++){
int best = r;
for(int i = r; i < n; i++){
if(abs(a[i][c]) > abs(a[best][c])) best = i;
}
if(abs(a[best][c]) < eps) continue;
for(int i = 0; i < m; i++) swap(a[r][i], a[best][i]);
swap(b[r], b[best]);
poz[c] = r;
for(int i = 0; i < n; i++){
if(i != r){
double temp = a[i][c] / a[r][c];
for(int j = c; j < m; j++) a[i][j] -= a[r][j] * temp;
b[i] -= b[r] * temp;
}
}
r++;
}
for(int i = 0; i < m; i++){
if(poz[i] != -1) sol[i] = b[poz[i]] / a[poz[i]][i];
}
for(int i = 0; i < n; i++){
double temp = 0;
for(int j = 0; j < m; j++) temp += sol[j] * a[i][j];
if(abs(temp - b[i]) > eps) return 0;
}
return 1;
}
int main()
{
int i;
fin >> n >> m;
a = vector < vector <double> > (n, vector <double> (m, 0));
b.assign(n, 0);
for(i = 0; i < n; i++){
for(int j = 0; j < m; j++) fin >> a[i][j];
fin >> b[i];
}
if(!gauss()){
fout << "Imposibil";
return 0;
}
for(auto x : sol){
fout << fixed << setprecision(10);
fout << x << " ";
}
return 0;
}