之前数值计算学过,可是好久不复习给忘记了。于是拿出出来复习。终于搞定。
题目意思很明显
给你一个方程组 让你求(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11)
但是方程组的形式是一个二次方程组
(ai1-x1)^2 + (ai2-x2)^2 +(ai3-x1)^2 + (ai4-x2)^2 +(ai5-x1)^2 + (ai6-x2)^2 +(ai7-x1)^2 + (ai8-x2)^2 + (ai9-x2)^2 +(ai10-x1)^2 + (ai11-x2)^2 = dis ^2
于是我们可以发现 用两个方程相减就可以得到一个一次方程组。 这样操作11次得到一个新的方程组,然后用高斯消元法求解即可。
Shirai Kuroko is a Senior One student. Almost everyone in Academy City have super powers, andKuroko is good at using it. Her ability is "Teleporting", which can make people to transfer in the eleven dimension, and it shows like moving instantly in general people's eyes.
In fact, the theory of the ability is simple. Each time, Kuroko will calculate the distance between some known objects and the destination in the eleven dimension so thatKuroko can get the coordinate of the destination where she want to go and use her ability.
Now we have known that the coordinate of twelve objects in the eleven dimensionVi = (Xi1,Xi2, ... ,Xi11), and 1 <= i <= 12. We also known that the distance Di between the destination and the object. Please write a program to calculate the coordinate of the destination. We can assume that the answer is unique and any four of the twelve objects are not on the same planar.
Input
The first line contains an integer T, means there are T test cases. For each test case, there are twelve lines, each line contains twelve real numbers, which meansXi1,Xi2, ... ,Xi11 andDi. T is less than 100.
Output
For each test case, you need to output eleven real numbers, which shows the coordinate of the destination. Round to 2 decimal places.
Sample Input
1 1.0 0 0 0 0 0 0 0 0 0 0 7.0 0 1.0 0 0 0 0 0 0 0 0 0 7.0 0 0 1.0 0 0 0 0 0 0 0 0 7.0 0 0 0 1.0 0 0 0 0 0 0 0 7.0 0 0 0 0 1.0 0 0 0 0 0 0 7.0 0 0 0 0 0 1.0 0 0 0 0 0 7.0 0 0 0 0 0 0 1.0 0 0 0 0 7.0 0 0 0 0 0 0 0 1.0 0 0 0 7.0 0 0 0 0 0 0 0 0 1.0 0 0 7.0 0 0 0 0 0 0 0 0 0 1.0 0 7.0 0 0 0 0 0 0 0 0 0 0 1.0 7.0 7.0 0 0 0 0 0 0 0 0 0 0 11.0
Sample Output
-2.00 -2.00 -2.00 -2.00 -2.00 -2.00 -2.00 -2.00 -2.00 -2.00 -2.00
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define MAXN 15
double a[MAXN][MAXN],ans[MAXN];
bool l[MAXN];
inline int gauss(int n)
{
int res=0,r=0;
for(int i=0;i<n;i++) l[i]=false;
for(int i=0;i<n;i++)
{
//行交换
for(int j=r;j<n;j++)
if(fabs(a[j][i])>eps)
{
for(int k=i;k<=n;k++)
swap(a[j][k],a[r][k]);
break;
}
if(fabs(a[r][i])<eps)
{
res++;
continue;
}
//消元
for(int j=0;j<n;j++)
if(j!=r && fabs(a[j][i])>eps)
{
double tmp=a[j][i]/a[r][i];
for(int k=i;k<=n;k++)
{
a[j][k]-=tmp*a[r][k];
//这里注意精度问题
if(fabs(a[j][k])<eps)
a[j][k]=0;
}
}
l[i]=true;
r++;
}
for(int i=0;i<n;i++)
if(l[i])
for(int j=0;j<n;j++)
if(fabs(a[j][i])>0)
ans[i]=a[j][n]/a[j][i];
//自由未知量数量
return res;
}
int t;
double tmp[MAXN][MAXN];
int main()
{
scanf("%d",&t);
for(int k=0;k<t;k++)
{
for(int i=0;i<12;i++)
for(int j=0;j<12;j++)
scanf("%lf",&tmp[i][j]);
for(int i=0;i<11;i++)
{
a[i][11]=0;
for(int j=0;j<11;j++)
{
a[i][j]=2*(tmp[i][j]-tmp[11][j]);
a[i][11]+=tmp[i][j]*tmp[i][j];
a[i][11]-=tmp[11][j]*tmp[11][j];
}
a[i][11]=a[i][11]-tmp[i][11]*tmp[i][11]+tmp[11][11]*tmp[11][11];
}
gauss(11);
for(int i=0;i<11;i++)
printf("%.2lf%s",ans[i],i!=10?" ":"\n");
}
return 0;
}