• 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;
    }
    

  • 相关阅读:
    USACO 5.1 Starry Night
    USACO 4.4 Frame Up
    USACO 4.4 Shuttle Puzzle
    USACO 4.3 Letter Game (字典树)
    USACO 4.3 Street Race
    BZOJ 1036: [ZJOI2008]树的统计Count (树链剖分模板题)
    BZOJ 1861: [Zjoi2006]Book 书架 (splay)
    codeforces 354 D. Transferring Pyramid
    codeforces 286 E. Ladies' Shop (FFT)
    USACO 4.3 Buy Low, Buy Lower
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11259993.html
Copyright © 2020-2023  润新知