高斯消元模板题
把第i列除了第i行外所有的系数变成0
#include <bits/stdc++.h> #define inf 2333333333333333 #define N 110 #define p(a) putchar(a) #define For(i,a,b) for(int i=a;i<=b;++i) //by war //2020.9.10 using namespace std; int n; double t,eps=1e-7; double a[N][N]; void in(int &x){ int y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(int x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } void gs(){ For(i,1,n){ t=a[i][i]; For(j,i+1,n) t=fmax(t,a[j][i]); if(fabs(t)<eps){ puts("No Solution"); return ; } For(j,1,n){ if(i!=j){ double temp=a[j][i]/a[i][i]; For(k,i,n+1){ a[j][k]-=a[i][k]*temp; } } } } For(i,1,n) a[i][n+1]/=a[i][i]; For(i,1,n) printf("%.2lf\n",a[i][n+1]); } signed main(){ in(n); For(i,1,n) For(j,1,n+1) scanf("%lf",&a[i][j]); gs(); return 0; }