80 pts WA求调

P2455 [SDOI2006] 线性方程组

lovely_codingcow @ 2023-10-11 14:15:01

WA on #3 #9

貌似是判不出无解和无数解,求调

#include <iostream>
#include <cmath>

using namespace std;

int n;
double a[59][59], b[59];

void gaussianElimination() {
    for (int i = 1; i <= n; i++) {
        int p = i;
        for (int j = i + 1; j <= n; j++) {
            if (fabs(a[j][i]) >= fabs(a[p][i])) {
                p = j;
            }
        }
        swap(a[i], a[p]);
        swap(b[i], b[p]);
        if (fabs(a[i][i]) < 1e-12) {
            if (fabs(b[i]) < 1e-12) {
                cout << "-1";
            } else {
                cout << "0";
            }
            exit(0);
        }
        double co = a[i][i];
        for (int j = i; j <= n; j++) {
            a[i][j] /= co;
        }
        b[i] /= co;
        for (int j = i + 1; j <= n; j++) {
            double fac = a[j][i];
            for (int k = i; k <= n; k++) {
                a[j][k] -= fac * a[i][k];
            }
            b[j] -= fac * b[i];
        }
    }
    for (int i = n; i >= 1; i--) {
        for (int j = i - 1; j >= 1; j--) {
            double fac = a[j][i];
            for (int k = i; k <= n; k++) {
                a[j][k] -= fac * a[i][k];
            }
            b[j] -= fac * b[i];
        }
    }
}

int main() {
//  freopen("9.in", "rt", stdin);
//  freopen("9.out", "wt", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> a[i][j];
        }
        cin >> b[i];
    }
    gaussianElimination();
    for (int i = 1; i <= n; i++) {
        cout << "x" << i << "=";
        printf("%.2lf\n", b[i]); 
    }
}

by Noble_Wolf @ 2023-12-15 16:53:42

Cuball


|