Mai intai trebuie sa te autentifici.

Cod sursa(job #1802764)

Utilizator DjokValeriu Motroi Djok Data 10 noiembrie 2016 17:13:51
Problema Algoritmul lui Gauss Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include<bits/stdc++.h>
using namespace std;

const double EPS = 1e-9;

int i, j, n, m, col, row, pivot, where[305];
double a[305][305], ans[305], aux;

int main()
{
  ifstream cin("gauss.in");
  ofstream cout("gauss.out");

  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(i = 1; i <= n; ++i)
    for(j = 1; j <= m + 1; ++j)
      cin >> a[i][j];

  memset(where, -1, sizeof(where));

  for(row = col = 1; col <= m && row <= n; ++col) {
    for(pivot = i = row; i <= n; ++i) if(abs(a[i][col]) > abs(a[pivot][col])) pivot = i;

    if(abs(a[pivot][col]) < EPS) continue;

    for(i = col; i <= m + 1; ++i) swap(a[pivot][i], a[row][i]);

    where[col] = row;

    for(i = 0; i <= n; ++i) {
      if(i == row) continue;

      aux = a[i][col] / a[row][col];
      for(j = col; j <= m + 1; ++j) a[i][j] -= a[row][j] * aux;
    }

    ++row;
  }

  for(i = 1; i <= m; ++i) if(where[i] != -1) ans[i] = a[where[i]][m + 1] / a[where[i]][i];

  for(i = 1; i <= n; ++i) {
    for(aux = 0, j = 1; j <= m; ++j) aux += ans[j] * a[i][j];

    if(abs(aux - a[i][m + 1]) > EPS) return cout << "Imposibil\n", 0;
  }

  for(i = 1; i <= m; ++i) cout << setprecision(8) << fixed << ans[i] << " \n"[i == m];

  return 0;
}