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

    传送门

     1 //minamoto
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cmath>
     5 using namespace std;
     6 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
     7 char buf[1<<21],*p1=buf,*p2=buf;
     8 inline int read(){
     9     #define num ch-'0'
    10     char ch;bool flag=0;int res;
    11     while((ch=getc())>'9'||ch<'0')
    12     (ch=='-')&&(flag=true);
    13     for(res=num;(ch=getc())<='9'&&ch>='0';res=res*10+num);
    14     (flag)&&(res=-res);
    15     #undef num
    16     return res;
    17 }
    18 const int N=105;
    19 double mp[N][N],ans[N],eps=1e-7;
    20 int main(){
    21 //    freopen("testdata.in","r",stdin);
    22     int n=read();
    23     for(int i=1;i<=n;++i)
    24     for(int j=1;j<=n+1;++j)
    25     mp[i][j]=read();
    26     for(int i=1;i<=n;++i){
    27         int r=i;
    28         for(int j=i+1;j<=n;++j)
    29         if(fabs(mp[r][i])<fabs(mp[j][i])) r=j;
    30         if(fabs(mp[r][i])<eps) return puts("No Solution"),0;
    31         if(i!=r) swap(mp[i],mp[r]);
    32         double div=mp[i][i];
    33         for(int j=i;j<=n+1;++j) mp[i][j]/=div;
    34         for(int j=i+1;j<=n;++j){
    35             div=mp[j][i];
    36             for(int k=i;k<=n+1;++k)
    37             mp[j][k]-=mp[i][k]*div;
    38         }
    39     }
    40     ans[n]=mp[n][n+1];
    41     for(int i=n-1;i;--i){
    42         ans[i]=mp[i][n+1];
    43         for(int j=i+1;j<=n;++j)
    44         ans[i]-=mp[i][j]*ans[j];
    45     }
    46     for(int i=1;i<=n;++i) printf("%.2lf
    ",ans[i]);
    47     return 0;
    48 }
  • 相关阅读:
    接口自动化架构-获取用例
    Windows性能监控工具Perfmon使用指南
    接口自动化架构1-setting
    多进程
    线程锁、守护线程
    多线程
    xlrd模块
    封装写日志的类
    封装redis
    继承
  • 原文地址:https://www.cnblogs.com/bztMinamoto/p/9729210.html
Copyright © 2011-2022 走看看