【BZOJ】【1013】【JSOI2008】球形空间产生器sphere
高斯消元
高斯消元模板题
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /************************************************************** 2 Problem: 1013 3 User: Tunix 4 Language: C++ 5 Result: Accepted 6 Time:0 ms 7 Memory:1272 kb 8 ****************************************************************/ 9 10 //BZOJ 1013 11 #include<cmath> 12 #include<cstdio> 13 #include<cstring> 14 #include<cstdlib> 15 #include<iostream> 16 #include<algorithm> 17 #define rep(i,n) for(int i=0;i<n;++i) 18 #define F(i,j,n) for(int i=j;i<=n;++i) 19 #define D(i,j,n) for(int i=j;i>=n;--i) 20 using namespace std; 21 void read(int &v){ 22 v=0; int sign=1; char ch=getchar(); 23 while(ch<'0'||ch>'9'){ if (ch=='-') sign=-1; ch=getchar();} 24 while(ch>='0'&&ch<='9'){ v=v*10+ch-'0'; ch=getchar();} 25 v*=sign; 26 } 27 /******************tamplate*********************/ 28 const int N=11; 29 typedef double Matrix[N][N]; 30 void gauss(Matrix a,int n){ 31 int i,j,k,r; 32 for(i=0;i<n;++i){ 33 //选一行r并与第i行交换 34 r=i; 35 for(j=i+1;j<n;++j) 36 if (fabs(a[j][i]) > fabs(a[r][i])) r=j; 37 if (r!=i) for(j=0;j<=n;++j) swap(a[r][j],a[i][j]); 38 39 //与第i+1~n行进行消元 40 for(k=i+1;k<n;++k){ 41 double f=a[k][i]/a[i][i];//为了让a[k][i]=0,第i行所乘的倍数 42 for(j=i;j<=n;++j) a[k][j]-=f*a[i][j]; 43 } 44 } 45 //回代过程 46 for(i=n-1;i>=0;i--){ 47 for(j=i+1;j<n;j++) 48 a[i][n]-=a[j][n]*a[i][j]; 49 a[i][n]/=a[i][i]; 50 } 51 } 52 Matrix f; 53 int n; 54 int main(){ 55 scanf("%d",&n); 56 F(i,0,n){ 57 rep(j,n){ 58 scanf("%lf",&f[i][j]); 59 f[i][n]-=f[i][j]*f[i][j]; 60 f[i][j]*=-2.0; 61 if (i) f[i-1][j]-=f[i][j]; 62 } 63 if (i) f[i-1][n]-=f[i][n]; 64 } 65 gauss(f,n); 66 printf("%.3f",f[0][n]); 67 for(int i=1;i<n;++i) printf(" %.3f",f[i][n]); 68 return 0; 69 }