zcmimi's blog
avatar
zc
2020-02-01 16:21:00
  • 本文总阅读量
查看原题

点击跳转

#include<bits/stdc++.h>
using namespace std;
#define FOR(i,x,y) for(int i=x;i<=y;++i)
int n;
double a[111][111],eps=1e-8;
bool cmp(int k,int x,int y){
    if(abs(abs(a[x][k])-abs(a[y][k]))>eps)return abs(a[x][k])>abs(a[y][k]);
    FOR(i,k+1,n)if(abs(abs(a[x][i])-abs(a[y][i]))>eps)return abs(a[x][i])>abs(a[y][i]);
    return 0;
}
int main(){
    scanf("%d",&n);
    FOR(i,1,n)FOR(j,1,n+1)scanf("%lf",&a[i][j]);
    int p=1;
    FOR(i,1,n){
        int mx=p;
        FOR(j,p+1,n)if(abs(a[j][i])>abs(a[mx][i]))mx=j;
        if(!a[mx][i])continue;
        std::swap(a[p],a[mx]);
        FOR(j,1,n)if(j!=p){
            double t=a[j][i]/a[i][i];
            FOR(k,i+1,n+1)
                a[j][k]-=a[i][k]*t;
        }
    }
    FOR(i,1,n)if(abs(a[i][i])<eps){
        if(abs(a[i][n+1])<eps)return printf("0\n"),0;
        else return printf("-1\n"),0;
    }
    FOR(i,1,n){
        printf("x%d=",i);
        double t=a[i][n+1]/a[i][i];
        if(abs(t)<eps)printf("0\n");
        else printf("%.2f\n",t);
    }
}
LG 2455 [SDOI2006]线性方程组
comment评论
Search
search