Cod sursa(job #1820015)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 1 decembrie 2016 02:28:32
Problema Algoritmul lui Gauss Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.78 kb
#include <bits/stdc++.h>
using namespace std;
typedef long double f80;

const int BMAX = 1 << 17,
          SPQR = 305;
const f80 EPS = 1e-9;

int n, m;
char buck[BMAX];
int point[SPQR];
f80 ant[SPQR], gauss[SPQR][SPQR];


inline char nextch(void) {
    static int buff = BMAX;
    if (buff == BMAX) {
        buff = 0;
        fread(buck, 1, BMAX, stdin); }
    return buck[ buff++ ]; }

void get(int &arg) {
    char ch;
    int sgn;

    arg = 0;
    sgn = 1;

    do {
        ch = nextch(); }
    while ((ch < '0' || ch > '9') && ch != '-');

    if (ch == '-') {
        sgn = -1;
        ch = nextch(); }

    do {
        arg = arg * 10 + ch - '0';
        ch = nextch(); }
    while (ch >= '0' && ch <= '9');

    arg*= sgn; }

inline bool eq(const f80 &a, const f80 &b) {
    return abs(a - b) < EPS; }

int main(void) {
    freopen("gauss.in", "r", stdin);
    freopen("gauss.out", "w", stdout);
    int i, j, k, tmp;
    f80 rat;

    get(n), get(m);
    for (i = 1; i <= n; ++i) {
        for (j = 1; j <= m + 1; ++j) {
            get(tmp);
            gauss[i][j] = tmp; } }

    for (i = 1; i <= n; ++i) {
        for (j = 1; j <= m + 1 && !point[i]; ++j) if (!eq(gauss[i][j], 0.0L))
            point[i] = j;

        if (point[i] == 0)
            continue;

        if (point[i] == m + 1) {
            puts("Imposibil\n");
            return 0; }

        for (k = 1; k <= n; ++k) if (i != k && !eq(gauss[k][point[i]], 0.0L)) {
            rat = gauss[k][point[i]] / gauss[i][point[i]];
            for (j = 1; j <= m + 1; ++j)
                gauss[k][j]-= gauss[i][j] * rat; } }

    for (i = 1; i <= n; ++i)
        if (point[i])
            ant[point[i]] = gauss[i][m + 1] / gauss[i][point[i]];

    for(i = 1; i <= m; ++i)
        printf("%.10Lf ", ant[i]);
    printf("\n");

    return 0; }