#include<bits/stdc++.h> #define re return #define inc(i,l,r) for(int i=l;i<=r;++i) #define dec(i,l,r) for(int i=l;i>=r;--i) const double EPS=1e-7; char buf[1<<21],*p1,*p2; inline int getc(){re p1==p2 and (p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;} template<typename T>inline void rd(T&x) { char c;bool f=0; while((c=getc())<'0'||c>'9')if(c=='-')f=1; x=c^48; while((c=getc())>='0' and c<='9')x=x*10+(c^48); if(f)x=-x; } using namespace std; double a[105][105],ans[105]; int n; int main() { // freopen("in.txt","r",stdin); rd(n); inc(i,1,n)inc(j,1,n+1)rd(a[i][j]); inc(i,1,n){ int r=i; if(fabs(a[r][r])<EPS){ inc(j,i+1,n) if(a[j][r]){swap(a[i],a[j]);break;} if(fabs(a[i][i])<EPS){printf("No Solution"); re 0;}//存在自由元 } double div=a[i][i]; inc(j,i,n+1)a[i][j]/=div;//这一行系数化为1,1-i-1都已是0 inc(j,i+1,n){ div=a[j][i]; inc(k,i,n+1)a[j][k]-=a[i][k]*div;//弄去剩下行(i)系数 //但可能一不小心弄多(把某一列的z系数也化没有了)了 } } dec(i,n,1){ ans[i]=a[i][n+1];//得解 dec(j,i-1,1)a[j][n+1]-=a[j][i]*ans[i];//代回去 } inc(i,1,n) printf("%.2lf\n",ans[i]); re 0; }
线性函数,加减消元