This article is made by Jason-Cow.
Welcome to reprint.
But please post the writer‘s address.
http://www.cnblogs.com/JasonCow/
推方程设n位坐标视为方程组的n个未知数
然后化为一般式。
#include <cmath> #include <cstdio> #include <algorithm> using namespace std; typedef double db; db a[15][15],b[15]; bool Guass(int n); bool work(); int main(){return work();} bool work(){ int n;scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%lf",&b[i]); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ db x;scanf("%lf",&x); a[i][j]+=2*(b[j]-x); a[i][n+1]+=(b[j]*b[j])-(x*x); } return Guass(n); } bool Guass(int n){ for(int i=1;i<=n;i++){ db Max=-1e9;int I=i; for(int j=i;j<=n;j++)if(fabs(a[i][j])>Max)Max=fabs(a[j][i]),I=j; if(I!=i)for(int j=1;j<=n+1;j++)swap(a[i][j],a[I][j]); db t=a[i][i]; for(int j=i;j<=n+1;j++)a[i][j]/=t; for(int j=1;j<=n;j++) if(i!=j){ db t=a[j][i]; for(int k=1;k<=n+1;k++)a[j][k]-=t*a[i][k]; } } for(int i=1;i<n;i++)printf("%.3lf ",a[i][n+1]);printf("%.3lf\n",a[n][n+1]); return 0; }
时间: 2024-10-31 17:47:01