Pagini recente » Cod sursa (job #522218) | Cod sursa (job #2030867) | Cod sursa (job #2590331) | Cod sursa (job #2055728) | Cod sursa (job #3041663)
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream fin("gauss.in");
ofstream fout("gauss.out");
const int NMAX = 301;
const double EPS = 1e-8;
double coef[NMAX + 1][NMAX + 1];
bool notNull(double x){
return (x < -EPS || x > EPS);
}
double sol[NMAX + 1];
int main(){
int n, m;
fin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m + 1; j++){
fin >> coef[i][j];
}
}
int i = 1, j = 1;
while(i <= n && j <= m){
int k = -1;
for(int ind = i; ind <= n; ind++){
if(notNull(coef[ind][j])){
k = ind;
break;
}
}
if(k == -1){
++j;
continue;
}
for(int col = 1; col <= m + 1; col++){
swap(coef[i][col], coef[k][col]);
}
for(int col = m + 1; col >= j; col--){
coef[i][col] /= coef[i][j];
}
for(int ind = i + 1; ind <= n; ind++){
for(int col = m + 1; col >= j; col--){
coef[ind][col] -= coef[i][col] * coef[ind][j];
}
}
i++, j++;
}
for(int ind = 1; ind <= n; ind++){
for(int col = 1; col <= m; col++){
if(notNull(coef[ind][col])){
sol[col] = coef[ind][m + 1] / coef[ind][col];
}
}
}
fout << fixed << setprecision(8);
for(int ind = n; ind >= 1; ind--){
for(int col = 1; col <= m + 1; col++){
if(notNull(coef[ind][col])){
if(col == m + 1){
fout << "Imposibil\n";
return 0;
}
sol[col] = coef[ind][m + 1];
for(int col2 = col + 1; col2 <= m; col2++){
sol[col] -= coef[ind][col2] * sol[col2];
}
break;
}
}
}
for(int col = 1; col <= m; col++){
fout << sol[col] << ' ';
}
fout << '\n';
return 0;
}