求助,错了#4和#8

P2455 [SDOI2006] 线性方程组

Inf_Love @ 2021-01-21 21:57:43

RT,这份代码为啥会挂啊,有什么解决方案吗

#include <iostream>
#include <cmath>
#include <cstdio>
#include <ctime>
#define eps 1e-8
using namespace std;
double c[1010][1010],b[1010];
int n;
int main(){
    cin >> n;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= n;j++)cin >> c[i][j];
        cin >> b[i];
    }
    double rate;
    for(int i = 1;i <= n;i++){
        for(int j = i;j <= n;j++)
            if(fabs(c[j][i]) > eps)
                swap(c[i],c[j]),swap(b[i],b[j]);
        if(c[i][i] == 0)
            break;
        for(int j = 1;j <= n;j++){
            if(i == j)continue;
            rate = c[j][i] / c[i][i];
            for(int k = i;k <= n;k++)
                c[j][k] -= c[i][k] * rate;
            b[j] -= b[i] * rate;
        }
    }
    int wujie = 0,duojie = 0;
    for(int i = 1;i <= n;i++){
        if(fabs(c[i][i]) < eps && fabs(b[i]) > eps)
            wujie = 1;
        if(fabs(c[i][i]) < eps && fabs(b[i]) < eps)
            duojie = 1;
    }
    if(wujie == 1){
        cout << -1 << endl;
        return 0;
    }
    if(duojie == 1){
        cout << 0 << endl;
        return 0;
    }
    for(int i = 1;i <= n;i++){
        printf("x%d=",i);
        if(fabs(b[i]) < eps)printf("0 \n");
        else printf("%.2f \n",b[i] / c[i][i]);
    }
    return 0;
}

by cxm1024 @ 2021-09-20 07:48:56

Cu Ball


|