现在的位置: 首页 > 综合 > 正文

球形空间产生器 Sphere

2018年01月15日 ⁄ 综合 ⁄ 共 1198字 ⁄ 字号 评论关闭

球形空间产生器

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.
【上篇】
【下篇】

抱歉!评论已关闭.