zoj 3645(高斯—浮点型——模板)

来源:互联网 发布:移动4g网络覆盖范围 编辑:程序博客网 时间:2024/05/21 07:08
BiliBili

Time Limit: 2 Seconds Memory Limit: 65536 KB

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.

Railgun_Kuroko.jpg

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 distanceDi 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

11.0 0 0 0 0 0 0 0 0 0 0 7.00 1.0 0 0 0 0 0 0 0 0 0 7.00 0 1.0 0 0 0 0 0 0 0 0 7.00 0 0 1.0 0 0 0 0 0 0 0 7.00 0 0 0 1.0 0 0 0 0 0 0 7.00 0 0 0 0 1.0 0 0 0 0 0 7.00 0 0 0 0 0 1.0 0 0 0 0 7.00 0 0 0 0 0 0 1.0 0 0 0 7.00 0 0 0 0 0 0 0 1.0 0 0 7.00 0 0 0 0 0 0 0 0 1.0 0 7.00 0 0 0 0 0 0 0 0 0 1.0 7.07.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个未知数,用最后一个方程和前11个方程相减,消掉X^2,11个方程,11个未知数,可以解。。。。浮点型高斯,可以当模版用喽!!!

#include<iostream>#include<cstdlib>#include<stdio.h>#include<memory.h>#include<math.h>using namespace std;const double eps = 1e-12;double a[12][12];const int n=11;double data[12][12],x[12];bool free_x[12];int sgn(double x){    return (x>eps)-(x<-eps);}int gauss(){    double temp;   int i,j,k,max_r,col=0,equ=n,var=n;   int free_x_num,free_index;   memset(free_x,true,sizeof(free_x));   for(k=0;k<equ&&col<var;k++,col++)   {       max_r=k;       for(i=k+1;i<equ;i++)       {           if(sgn(fabs(a[i][col])-fabs(a[max_r][col]))>0)           max_r=i;       }       if(max_r!=k)       {           for(j=k;j<var+1;j++)           swap(a[k][j],a[max_r][j]);       }       if(sgn(a[k][col])==0)       {           k--;continue;       }       for(i=k+1;i<equ;i++)       {           if(sgn(a[i][col])!=0)           {               double t=a[i][col]/a[k][col];               for(j=col;j<var+1;j++)               a[i][j]-=a[k][j]*t;           }       }   }   if (k < var)    {        for (i = k - 1; i >= 0; i--)        {            free_x_num = 0;            for (j = 0; j < var; j++)            {                if ( sgn(a[i][j])!=0 && free_x[j]){                    free_x_num++, free_index = j;                }            }            if(free_x_num>1)    continue;            temp = a[i][var];            for (j = 0; j < var; j++)            {                if (sgn(a[i][j])!=0 && j != free_index) temp -= a[i][j] * x[j];            }            x[free_index] = temp / a[i][free_index];            free_x[free_index] = 0;        }        return var - k;    }   for(i=var-1;i>=0;i--)   {     double temp=a[i][var];     for(j=i+1;j<var;j++)     {         if(sgn(a[i][j])!=0)         temp-=a[i][j]*x[j];     }     x[i]=temp/a[i][i];   }   return 0;}int main(){    int t,i,j,sum;    scanf("%d",&t);    while(t--)    {        memset(x,0,sizeof(x));        memset(a,0,sizeof(a));        for(i=0;i<12;i++)        for(j=0;j<12;j++)        scanf("%lf",&data[i][j]);        sum=0;        for(i=0;i<11;i++)        sum+=data[11][i]*data[11][i];        for(i=0;i<11;i++)        {            for(j=0;j<11;j++)            {                a[i][j]=2*(data[i][j]-data[11][j]);                a[i][11]+=data[i][j]*data[i][j];            }            a[i][11]+=data[11][11]*data[11][11]-data[i][11]*data[i][11]-sum;        }        gauss();        printf("%.2lf",x[0]);        for(i=1;i<11;i++)        printf(" %.2lf",x[i]);        printf("\n");    }    return 0;}



原创粉丝点击