Pagini recente » Cod sursa (job #2070494) | Cod sursa (job #1079947) | Cod sursa (job #838510) | Cod sursa (job #2477256) | Cod sursa (job #2730356)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("gauss.in");
ofstream fout("gauss.out");
const double eps = 0.0000001;
int n, m;
int main() {
fin >> n >> m;
vector<vector<double>> sist(n, vector<double>(m+1));
for(int i = 0; i < n; i++)
for(int j = 0; j <= m;j ++)
fin >> sist[i][j];
int i = 0, j= 0;
while(i < n && j < m) {
/// gasim o ecuatie in care apare necunoscuta j
int k = n;
for(int ec = i; ec < n; ec++)
if(sist[ec][j] < -eps || sist[ec][j] > eps) {
k = ec;
break;
}
if(k == n) {
j++;
continue;
}
/// punem ecuatia gasita pe pozitia i
for(int l = 0; l <= m; l++)
swap(sist[i][l], sist[k][l]);
/// impartim coeficientii ecuatiei ai coeficientul lui j sa fie 1
for(int l = j+1; l <= m; l++)
sist[i][l] /= sist[i][j];
sist[i][j] = 1;
/// eliminam j din toate ecuatiile urmatoare
for(int e = i+1; e < n; e++) {
for(int l = j+1; l <= m; l++)
sist[e][l] -= sist[e][j]*sist[i][l];
sist[e][j] = 0;
}
i++, j++;
}
vector<double> ans(m);
for(int i = n-1; i >= 0; i--)
for(int j = 0; j <= m; j++)
if(sist[i][j] > eps || sist[i][j] < -eps) {
if(j == m) {
fout << "Imposibil\n";
return 0;
}
ans[j] = sist[i][m];
for(int k = j+1; k < m; k++)
ans[j] -= ans[k]*sist[i][k];
break;
}
for(int i = 0; i < m; i++)
fout << fixed << setprecision(10) << ans[i] << ' ';
}