天天看點

球形空間産生器 Sphere

球形空間産生器

Sphere/.IN/.OUT/.PAS/.EXE

【題意】

給出求在n維坐标系中,到n+1個點距離都相同的點

輸入

輸入檔案第一行為整數N,代表了空間的次元。

接下來N+1 行,每行N 個實數代表一個坐标。輸入資料精确到小數點後6 位。

輸入資料保證輸出結果唯一。

輸出

輸出一行N 個實數代表球心的坐标,精确到小數點後三位。相鄰的數字之間用一個空

格分開(行末無空格)。

由n+1個坐标可得出n個n元一次方程

高斯消元即可

program sphere;
const
  eps=1e-7;
type
  equation=array [0..11] of extended;
var
  n,i,j,k:longint;
  x:extended;
  temp:equation;
  square:array [0..11] of equation;
  point,con:array [0..11] of extended;

procedure swap (var a,b:equation);
var
  i:equation;
begin
  i:=a;
  a:=b;
  b:=i;
end;

begin
  assign(input,'sphere.in');
  reset(input);
  assign(output,'sphere.out');
  rewrite(output);
  read(n);
  for i:=1 to n do
    begin
      read(x);
      temp[i]:=-2*x;
      con[0]:=con[0]-x*x;
    end;
  for i:=1 to n do
    begin
      con[i]:=-con[0];
      for j:=1 to n do
        begin
          read(x);
          square[i][j]:=-2*x-temp[j];
          con[i]:=con[i]-x*x;
        end;
    end;
  for i:=1 to n-1 do
    begin
      if abs(square[i][i])<eps then
        for j:=i+1 to n do
          if abs(square[j][i])>eps then
            begin
              swap(square[i],square[j]);
              break;
            end;
      for j:=i+1 to n do
        if abs(square[j][i])>eps then
          begin
            x:=-square[j][i]/square[i][i];
            for k:=i to n do
              square[j][k]:=square[j][k]+square[i][k]*x;
            con[j]:=con[j]+con[i]*x;
          end;
    end;
  for i:=n downto 1 do
    begin
      x:=con[i];
      for j:=i+1 to n do
        x:=x-point[j]*square[i][j];
      point[i]:=x/square[i][i];
    end;
  for i:=1 to n do
    begin
      write(point[i]:0:3);
      if i<>n then write(' ');
    end;
  close(input);
  close(output);
end.
           

繼續閱讀