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.
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, ... ,Xi11and Di. T is less than 100.
For each test case, you need to output eleven real numbers, which shows the coordinate of the destination. Round to 2 decimal places.
题目大意:11维度上有一个未知的点,现在已知12个点的坐标和他们到这个未知的点的距离,求这个未知的点的坐标。
思路:设未知的点为(p1, p2, ……, pn)
那么对于每个已知点,列方程(xi1 - p1)^2 + (xi2 - p2)^2 + …… + (xin - pn)^2 = Di^2
然后,对于前11个方程,减去第12个方程,就能得到一个线性方程组。
然后高斯消元解即可。
PS:我的代码不加那个EPS会跪,我觉得以后没事还是把它加上吧……
代码(0MS):
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 8 const double EPS = 1e-12; 9 const int MAXN = 15; 10 11 double mat[MAXN][MAXN]; 12 int n = 11, T; 13 14 inline int sgn(double x) { 15 return (x > EPS) - (x < -EPS); 16 } 17 18 void guess_eliminatioin() { 19 for(int i = 0; i < n; ++i) { 20 int r = i; 21 for(int j = i + 1; j < n; ++j) 22 if(fabs(mat[j][i]) > fabs(mat[r][i])) r = j; 23 if(sgn(mat[r][i]) == 0) continue; 24 if(r != i) for(int j = 0; j <= n; ++j) swap(mat[r][j], mat[i][j]); 25 for(int j = n; j >= i; --j) 26 for(int k = i + 1; k < n; ++k) mat[k][j] -= mat[k][i] / mat[i][i] * mat[i][j]; 27 } 28 for(int i = n - 1; i >= 0; --i) { 29 for(int j = i + 1; j < n; ++j) 30 mat[i][n] -= mat[j][n] * mat[i][j]; 31 mat[i][n] /= mat[i][i]; 32 } 33 } 34 35 int main() { 36 scanf("%d", &T); 37 while(T--) { 38 for(int i = 0; i <= n; ++i) 39 for(int j = 0; j <= n; ++j) scanf("%lf", &mat[i][j]); 40 for(int i = 0; i < n; ++i) { 41 mat[i][n] = mat[i][n] * mat[i][n] - mat[n][n] * mat[n][n]; 42 for(int j = 0; j < n; ++j) { 43 mat[i][n] -= mat[i][j] * mat[i][j] - mat[n][j] * mat[n][j]; 44 mat[i][j] = -2 * mat[i][j] + 2 * mat[n][j]; 45 } 46 } 47 guess_eliminatioin(); 48 for(int i = 0; i < n - 1; ++i) printf("%.2f ", mat[i][n] + EPS); 49 printf("%.2f\n", mat[n - 1][n] + EPS); 50 } 51 }