Pagini recente » Cod sursa (job #3333695) | Cod sursa (job #1808122) | Cod sursa (job #1208171) | Cod sursa (job #3348597) | Cod sursa (job #3303108)
#include <bits/stdc++.h>
using namespace std;
const double eps = 0.00000001;
double v[305][305], ans[305];
int main(){
ifstream cin("gauss.in");
ofstream cout("gauss.out");
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m + 1; j++){
cin >> v[i][j];
}
}
int i, j, l, sl;
i=j=1;
while(i<=n&&j<=m) {
l=i;
while(l<=n) {
if(abs(v[l][j])>eps) {
sl=l;
l=n;
}
l++;
}
if(false) {
cout << "am avut dreptate";
} else {
if(sl!=i) {
swap(v[sl],v[i]);
}
double aux =v[i][j];
for(int c=j; c<=m+1; c++) {
v[i][c]/=aux;
}
for(l=i+1; l<=n; l++) {
for(int c=j+1; c<=m+1; c++) {
v[l][c]-=v[i][c]*v[l][j];
}
v[l][j]=0;
}
i++;
j++;
}
}
bool ok = 1;
for(int i = n; i >= 1; i--){
int j = 1;
while(j <= m + 1 && abs(v[i][j]) < eps)
j++;
if(j < m + 2){
if(j == m + 1){
ok = 0;
cout << "Imposibil\n";
break;
}
ans[j] = v[i][m + 1];
for(int jj = j + 1; jj <= m; jj++){
ans[i] -= ans[jj] * v[i][jj];
}
}
}
if(ok){
for(int i = 1; i <= m; i++)
cout << fixed << setprecision(10) << ans[i] << " ";
}
return 0;
}