zoukankan      html  css  js  c++  java
  • 高斯消元【模板】

    传送门:

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    #define ll long long
    #define re register
    const double eps=1e-8;
    inline void read(int &a)
    {
        a=0;
        int d=1;
        char ch;
        while(ch=getchar(),ch>'9'||ch<'0')
            if(ch=='-')
                d=-1;
        a=ch^48;
        while(ch=getchar(),ch>='0'&&ch<='9')
            a=(a<<3)+(a<<1)+(ch^48);
        a*=d;
    }
    double a[105][105],ans[105];
    int main()
    {
        int n;
        read(n);
        for(re int i=1;i<=n;i++)
            for(re int j=1;j<=n+1;j++)
                scanf("%lf",&a[i][j]);
        for(re int i=1;i<=n;i++)
        {
            int r=i;
            for(re int j=i+1;j<=n;j++)
                if(fabs(a[r][i])<fabs(a[j][i]))
                    r=j;
            if(fabs(a[r][i])<eps)
            {
                printf("No Solution
    ");
                return 0;
            }
            if(r!=i)
                swap(a[r],a[i]);
            double div=a[i][i];
            for(re int j=i;j<=n+1;j++)
                a[i][j]/=div;
            for(re int j=i+1;j<=n;j++)
            {
                div=a[j][i];
                for(re int k=i;k<=n+1;k++)
                    a[j][k]-=a[i][k]*div;
            }
        }
        ans[n]=a[n][n+1];
        for(re int i=n-1;i>=1;i--)
        {
            ans[i]=a[i][n+1];
            for(re int j=i+1;j<=n;j++)
                ans[i]-=a[i][j]*ans[j];
        }
        for(re int i=1;i<=n;i++)
            printf("%.2lf
    ",ans[i]);
        return 0;
    }
  • 相关阅读:
    NUnit进行单元测试
    VSTS 安装步骤
    使用 Visual Studio Team Test 进行单元测试
    vss使用技巧
    struts 2.1 action 学习
    apache2 反向代理
    zz mysql中文
    trac ubuntu 安装
    ejb 3中bean的种类
    linux下VsFTP配置全方案
  • 原文地址:https://www.cnblogs.com/acm1ruoji/p/10905544.html
Copyright © 2011-2022 走看看