bzoj1013 [JSOI2008]球形空间产生器sphere
第一次写高斯消元
注意考虑分母、分子为零的情况
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<cmath> 8 #include<ctime> 9 #include<queue> 10 #include<stack> 11 #include<map> 12 #include<set> 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--) 14 #define re(i,l,r) for(int i=(l);i<=(r);i++) 15 #define Clear(a,b) memset(a,b,sizeof(a)) 16 #define inout(x) printf("%d",(x)) 17 #define douin(x) scanf("%lf",&x) 18 #define strin(x) scanf("%s",(x)) 19 #define LLin(x) scanf("%lld",&x) 20 #define op operator 21 #define CSC main 22 typedef unsigned long long ULL; 23 typedef const int cint; 24 typedef long long LL; 25 using namespace std; 26 double f(const double &a){return a*a;} 27 const double eps=1e-8; 28 void inin(int &ret) 29 { 30 ret=0;int f=0;char ch=getchar(); 31 while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} 32 while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar(); 33 ret=f?-ret:ret; 34 } 35 int n; 36 double xl[11],temp; 37 double a[11][11],b[11],x[11]; 38 int main() 39 { 40 freopen("in.in","r",stdin); 41 freopen("out.out","w",stdout); 42 inin(n); 43 re(i,1,n)douin(xl[i]); 44 re(i,1,n)re(j,1,n) 45 { 46 douin(temp); 47 a[i][j]=2*(temp-xl[j]); 48 a[i][n+1]+=f(temp)-f(xl[j]); 49 } 50 re(i,1,n-1) 51 { 52 if(abs(a[i][i]<eps)) 53 re(j,i+1,n)if(abs(a[j][i])>eps) 54 { 55 re(k,1,n+1)a[i][k]+=a[j][k]; 56 break; 57 } 58 re(j,i+1,n) 59 { 60 if(abs(a[j][i])<eps)continue; 61 double bi=a[i][i]/a[j][i]; 62 re(k,i,n+1)a[j][k]*=bi; 63 re(k,i,n+1)a[j][k]-=a[i][k]; 64 } 65 } 66 rre(i,n,1) 67 { 68 x[i]=a[i][n+1]/a[i][i]; 69 rre(j,i-1,1)a[j][n+1]-=a[j][i]*x[i],a[j][i]=0; 70 } 71 re(i,1,n)printf("%.3f%c",x[i],i==n?'\n':' '); 72 return 0; 73 }