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


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
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个未知数,用最后一个方程和前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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值