Description
Shirai Kuroko is a Senior One student. Almost everyone in Academy City have super powers, and Kuroko 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 that Kuroko 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 dimension Vi = (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 means Xi1,Xi2, ... ,Xi11 and Di. 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
思路: 12个二次方程两两作差,可以得到11个线性方程,然后做一次高斯即可得到答案,需要注意的是,做guass的时候,由于有负数项存在,交换两行时,应该选择绝对值较高的那个,同时应该避免输出-0.00,因为这两点wa了好几次。
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const long M=20;
double a[M][M];
void guass(long n){
for (long i=0;i<n;++i){
long r=i;
for (long j=i+1;j<n;++j)
if (fabs(a[j][i])>fabs(a[r][i])) r=j;
for (long j=i;j<=n;++j) swap(a[i][j],a[r][j]);
for (long k=i+1;k<n;++k){
double rate=a[k][i]/a[i][i];
for (long j=i;j<=n;++j) a[k][j]-=rate*a[i][j];
}
}
for (long i=n-1;i>=0;--i){
for (long j=i+1;j<n;++j)
a[i][n]-=a[j][n]*a[i][j];
a[i][n]/=a[i][i];
}
}
double fix(double x){
if (fabs(x)<1e-8) return 0;
return x;
}
double mat[M][M],b[M];
long t;
int main(){
scanf("%d",&t);
while (t--){
for (long i=0;i<12;++i){
double res=0.0;
for (long j=0;j<11;++j){
scanf("%lf",&mat[i][j]);
res+=mat[i][j]*mat[i][j];
mat[i][j]*=(-2);
}
scanf("%lf",&b[i]);
b[i]=b[i]*b[i]-res;
}
for (long i=0;i<11;++i){
for (long j=0;j<11;++j)
a[i][j]=mat[i][j]-mat[i+1][j];
a[i][11]=b[i]-b[i+1];
}
guass(11);
for (long i=0;i<11;++i){
printf("%.2f",fix(a[i][11]));
if (i<10) printf(" ");
else printf("\n");
}
}
return 0;
}