Pagini recente » Cod sursa (job #2355586) | Cod sursa (job #872922) | Cod sursa (job #2622052) | Cod sursa (job #2974656) | Cod sursa (job #723392)
Cod sursa(job #723392)
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
const int maxn = 305;
const double eps = 0.000001;
inline bool zero(double x){
if(x < 0) x = -x;
return x < eps;
}
int n, m;
double A[maxn][maxn], X[maxn];
int main(){
freopen("gauss.in", "r", stdin);
freopen("gauss.out", "w", stdout);
scanf("%d %d\n", &n, &m);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m + 1; ++j)
scanf("%lf", &A[i][j]);
int i = 1, j = 1;
while(i <= n && j <= m) {
int l;
for(l = i; l <= n; ++l)
if(!zero(A[l][j])) break;
if(l == n + 1) {
++j; continue;
}
for(int k = 1; k <= m + 1; ++k) swap(A[i][k], A[l][k]);
for(int k = j + 1; k <= m + 1; ++k) A[i][k] /= A[i][j];
A[i][j] = 1.0;
for(int line = i + 1; line <= n; ++line) {
for(int k = j + 1; k <= m + 1; ++k)
A[line][k] -= A[i][k] * A[line][j];
A[line][j] = 0.0;
}
++i, ++j;
}
for(int i = n; i >= 1; --i)
for(int j = 1; j <= m + 1; ++j) {
if(!zero(A[i][j])) {
if(j == m + 1) {
printf("Imposibil\n");
return 0;
}
X[j] = A[i][m + 1];
for(int k = j + 1; k <= m; ++k)
X[j] -= X[k] * A[i][k];
break;
}
}
for(int j = 1; j <= m; ++j)
printf("%.8lf ", X[j]);
return 0;
}