• Luogu2455 [SDOI2006]线性方程组 (高斯消元)


    模板特殊情况没exit(0) $longrightarrow$60 了一下午

    //#include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
    #define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
    #define Max(a,b) ((a) > (b) ? (a) : (b))
    #define Min(a,b) ((a) < (b) ? (a) : (b))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Abs(a) ((a) < 0 ? -(a) : (a))
    #define Swap(a,b) a^=b^=a^=b
    #define ll long long
    
    #define ON_DEBUG
    
    #ifdef ON_DEBUG
    
    #define D_e_Line printf("
    
    ----------
    
    ")
    #define D_e(x)  cout << #x << " = " << x << endl
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt","r",stdin);
    
    #else
    
    #define D_e_Line ;
    #define D_e(x)  ;
    #define Pause() ;
    #define FileOpen() ;
    
    #endif
    
    struct ios{
        template<typename ATP>ios& operator >> (ATP &x){
            x = 0; int f = 1; char c;
            for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
            while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
            x*= f;
            return *this;
        }
    }io;
    using namespace std;
    
    const int N = 107;
    const double eps = 1e-8;
    
    int n;
    double a[N][N], ans[N];
    inline double fabs(double a){
    	return a < 0 ? -a : a;
    }
    inline void Gauss(){
    	R(i,1,n){
    		int r = i;
    		R(j,i + 1,n)
    			if(fabs(a[j][i]) > fabs(a[r][i]))
    				r = j;
    		if(i != r) swap(a[i], a[r]);
    		if(fabs(a[i][i]) > eps){
    			R(j,1,n){
    				if(i == j) continue;
    				double s = a[j][i] / a[i][i];
    				R(k,1,n + 1)
    					a[j][k] -= s * a[i][k];
    			}	
    		}
    	}
    	int flagNo = 0, flagInf = 0;
    	R(i,1,n){
    		int tot = 0;
    		R(j,1,n + 1){
    			if(fabs(a[i][j]) < eps)
    				++tot;
    			else
    				break;
    		}
    		if(tot == n + 1) flagInf = 1;
    		else if(tot == n && fabs(a[i][n + 1]) > eps) flagNo = 1;
    	}
    	if(flagNo == 1){ printf("-1"); exit(0);}
    	if(flagInf == 1){ printf("0"); exit(0);}
    	
    	nR(i,n,1){
    		ans[i] = a[i][n + 1] / a[i][i];
    		nR(j,i - 1,1){
    			a[j][n + 1] -= a[j][i] * ans[i];
    		}
    	}
    }
    
    int main(){
    	io >> n;
    	R(i,1,n){
    		R(j,1,n + 1){
    			scanf("%lf", &a[i][j]);
    		}
    	}
    	
    	Gauss();
    	
    	R(i,1,n){
    //		if(fabs(ans[i]) < eps)
    //			printf("x%d=0
    ", i);
    //		else
    			printf("x%d=%.2lf
    ", i, ans[i]);
    	}
            
    	return 0;
    }
    

  • 相关阅读:
    制衡技术的新蓝海
    制衡技术,从Adblock所想到的
    centos6中安装新版 Elasticsearch 7.x
    nrm 安装与npm镜像切换
    james2.3 配置收件 之 MariaDB数据库配置
    手动搭建apache james邮件服务器,实现邮件功能
    James 如何作为服务在后台启动
    安装最新版RabbitMQ v3.7.13 以及基本配置
    mac 下 通过 brew 安装 MariaDB
    Mac 上安装maven
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11259993.html
Copyright © 2020-2023  润新知