90pts求助

P2455 [SDOI2006] 线性方程组

黑影洞人 @ 2023-08-16 09:05:39

#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 55
using namespace std;
const double eps=1e-10;
int n;
double a[N][N];
void guess(){
    for(int i=1;i<=n;i++){
        int id=i;
        for(int j=i;j<=n;j++)if(fabs(a[j][i])>fabs(a[id][i]))id=j;
        if(fabs(a[id][i])>eps)for(int j=i;j<=n+1;j++)swap(a[i][j],a[id][j]);
        else continue;
        for(int j=n+1;j>=i;j--)a[i][j]/=a[i][i];
        for(int j=1;j<=n;j++){
            if(i==j)continue;
            for(int k=n+1;k>=i;k--)a[j][k]-=a[j][i]*a[i][k];
        }
    }
    for(int i=1;i<=n;i++)if(fabs(a[i][i])<eps&&fabs(a[i][n+1])>eps)return puts("-1"),void();
    for(int i=1;i<=n;i++)if(fabs(a[i][i])<eps)return puts("0"),void();
    for(int i=1;i<=n;i++)printf("x%d=%.2lf\n",i,a[i][n+1]);
}
signed main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)for(int j=1;j<=n+1;j++)scanf("%lf",&a[i][j]);
    guess();
    return 0;
}

by AlexSong @ 2023-08-16 09:13:21

求关


#include<bits/stdc++.h>
using namespace std;
const int N = 55;
const double eps = 1e-8;
double a[N][N];
int n;
int gauss(){
    int r,c;
    for(c = 0,r = 0;c<n;c++){
        int t = r;
        for(int i = r;i<n;i++)
            if(fabs(a[i][c])>fabs(a[t][c]))t = i;
        if(fabs(a[t][c])<eps)continue;
        for(int i = c;i<=n;i++)swap(a[r][i],a[t][i]);
        for(int i = n;i>=c;i--)a[r][i]/=a[r][c];
        for(int i = r+1;i<n;i++){
            if(fabs(a[i][c])>eps){
                for(int j = n;j>=c;j--){
                    a[i][j] -= a[i][c] * a[r][j];
                }
            }
        }
        r++;
    }
    if(r<n){
        for(int i = r;i<n;i++){
            if(fabs(a[i][n])>eps)return 2;
        return 1;
        }
    }
    for(int i = n-1;i>=0;i--)
        for(int j = i+1;j<n;j++)a[i][n] -= a[i][j]*a[j][n];
    return 0;
}
int main(){
    scanf("%d",&n);
    for(int i = 0;i<n;i++)
        for(int j = 0;j<=n;j++)scanf("%lf",&a[i][j]);
    int t = gauss();
    if(t==2)return puts("-1"),0;
    if(t==1)return puts("0"),0;
    for(int i = 0;i<n;i++){
        cout<<"x"<<i+1<<"=";
        printf("%.2lf\n",a[i][n]);
    }
    return 0;
}

by xigou_zmx @ 2023-08-16 09:15:41

@AlexSong 你 tm 拿一个题解给人家还求关?


by zyxxxxxxxxxx @ 2023-08-16 09:19:33

不会,但是来露个头


by xigou_zmx @ 2023-08-16 09:19:56

我感觉应该是要特判? @黑影洞人


by 黑影洞人 @ 2023-08-16 09:28:29

@xigou_zmx 我特判了呀


by 黑影洞人 @ 2023-08-16 09:33:11

@AlexSong 要脸吗?


by ForwardStar @ 2023-10-13 10:44:52

@黑影洞人 阿米诺斯


by ForwardStar @ 2023-10-13 10:45:30

@zyxxxxxxxxxx 阿米诺斯,怎么哪里都能碰到你


|