一直80分,WA#4#8求助

P2455 [SDOI2006] 线性方程组

Sangber @ 2020-03-15 19:34:56

RT ,这是 80 分代码。

#include <algorithm>
#include <cstdio>
#include <cmath>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while ('0' > c || c > '9') f |= c == '-', c = getchar();
    while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
    s = f ? -s : s;
}

const int _ = 55;
const double eps = 1e-2;

int n, id[_]; double a[_][_], x[_];

int main() {
#ifndef ONLINE_JUDGE
    file("cpp");
#endif
    read(n);
    for (rg int i = 1; i <= n; ++i)
        for (rg int j = 1; j <= n + 1; ++j) read(a[i][j]);
    for (rg int i = 1; i <= n; ++i) {
        int r = i;
        for (rg int j = i + 1; j <= n; ++j)
            if (fabs(a[r][i]) < fabs(a[j][i])) r = j;
        if (fabs(a[r][i]) < eps) continue ;
        for (rg int j = 1; j <= n + 1; ++j) swap(a[r][j], a[i][j]);
        double div = a[i][i];
        for (rg int j = i; j <= n + 1; ++j) a[i][j] /= div;
        for (rg int j = i + 1; j <= n; ++j) {
            div = a[j][i];
            for (rg int k = i; k <= n + 1; ++k) a[j][k] -= a[i][k] * div;
        }
    }
    for (rg int i = 1; i <= n; ++i) if (fabs(a[i][i]) < eps && fabs(a[i][n + 1]) > eps) { puts("-1"); return 0; }
    for (rg int i = 1; i <= n; ++i) if (fabs(a[i][i]) < eps && fabs(a[i][n + 1]) < eps) { puts("0 "); return 0; }
    x[n] = a[n][n + 1];
    for (rg int i = n - 1; i >= 1; --i) {
        x[i] = a[i][n + 1];
        for (rg int j = i + 1; j <= n; ++j) x[i] -= x[j] * a[i][j];
    }
    for (rg int i = 1; i <= n; ++i) printf("x%d=%.2lf\n", i, x[i]);
    return 0;
}

by dz_ice @ 2020-03-15 22:31:44

马蜂毒瘤(


|