球形空间产生器sphere HYSBZ - 1013 (高斯消元)

原题地址

题意

给出n维的球上的n个点,问原球体球心。

提示

n维球体上两点距离公式\(dist = \sqrt{ (a1-b1)^2 + (a2-b2)^2 + … + (an-bn)^2 }\)

解法

\((x1-x0)^2\) --1

\((x2-x0)^2\) --2

2-1得

\((x2-x0)^2-(x1-x0)^2=0\)

-->

\(2(x2-x1)x0=(x2-x1)^2\)

类似可得

\(2(x2-x1)x0+2(y2-y1)y0+....=(x2-x1)^2+(y2-y1)^2....\)

n+1个点,可以得到n的方程,由此可以解出n个变元

参考代码一

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 40;
int equ,var;
double a[N][N];
double x[N];
double free_x[N];
double c[15][15];
int free_num;
const double eps=1e-9;
double Gauss()
{
int row,col,max_r;
int i,j;
row = col = 0;
while(row < equ && col < var)
{
max_r = row;
for(i = row+1; i < equ; i++)
if(fabs(a[i][col])-fabs(a[max_r][col]) > eps)
max_r = i; if(max_r != row)
{
for(j = col; j <= var; j++)
swap(a[row][j],a[max_r][j]);
}
if(fabs(a[row][col]) < eps)
{
col++;
continue;
} for(i = row+1; i < equ; i++)
{
if(fabs(a[i][col]) > eps)
{
double t = a[i][col]/a[row][col];
a[i][col] = 0.0; for(j = col+1; j <= var; j++)
a[i][j] -= a[row][j]*t;
}
}
row++;
col++;
}
//唯一解,回代
for(int i=equ-1;i>=0;i--)
{
x[i]=a[i][var];
for(int j=i+1;j<var;j++) x[i]-=a[i][j]*x[j];
x[i]/=a[i][i];
} return 0;
}
void init()
{
memset(a,0,sizeof(a));
memset(x,0,sizeof(x));
} int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<=n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%lf",&c[i][j]);
}
} for(int i=1;i<=n;i++)
{
for(int j=0;j<n;j++)
{
a[i-1][j]=2*(c[i][j]-c[i-1][j]);
}
a[i-1][n]=0;
for(int j=0;j<n;j++) a[i-1][n]+=(c[i][j]+c[i-1][j])*(c[i][j]-c[i-1][j]);
}
equ=n;var=n;
Gauss();
for(int i=0;i<n;i++)
{
if(i) printf(" ");
printf("%.3f",x[i]);
}
}
return 0;
}

参考代码二

/**************************************************************
Problem: 1013
User: yueguang12
Language: C++
Result: Accepted
Time:0 ms
Memory:1296 kb
****************************************************************/ #include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 30005
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void Out(ll a){
if(a<0) putchar('-'),a=-a;
if(a>=10) Out(a/10);
putchar(a%10+'0');
}
const int N=15;
double a[N][N];
double c[15][15];
const double eps=1e-9;
int n;
bool Gauss(){
int now=1,to;double t;
for(int i=1;i<=n;i++){
for(to=now;to<=n;to++)if(fabs(a[to][i])>eps)break;
if(to>n)continue;
if(to!=now)for(int j=1;j<=n+1;j++)
swap(a[to][j],a[now][j]);
t=a[now][i];
for(int j=1;j<=n+1;j++)a[now][j]/=t;
for(int j=1;j<=n;j++) if(j!=now){
t=a[j][i];
for(int k=1;k<=n+1;k++)
a[j][k]-=t*a[now][k];
}
now++;
}
for(int i=now;i<=n;i++)
if(fabs(a[i][n+1])>eps) return 0;
return 1;
}
void Build(){
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)
{
a[i][j]=2*(c[i][j]-c[i-1][j]);
a[i][n+1]+=(c[i][j]+c[i-1][j])*(c[i][j]-c[i-1][j]);
}
}
int main(){
n=read();
for(int i=0;i<=n;i++) for(int j=1;j<=n;j++)
scanf("%lf",&c[i][j]);
Build();
Gauss();
for(int i=1;i<=n;i++){
if(i>1) printf(" ");
printf("%.3f",a[i][n+1]);
}
puts("");
return 0;
}

最新文章

  1. 使用快捷键提升C#开发效率
  2. Linux文件操作常用命令整理
  3. Java之POJO
  4. 关于php的一些小知识
  5. [拇指飞动]构建高性能Web站点(1)
  6. HDU-4678 Mine 博弈SG函数
  7. Android ADT离线更新办法
  8. Oracle三组难缠的hint no_unnest/unnest,push_subq,push_pred--平展化(转)
  9. smtp中ehlo的使用
  10. .NET面试题系列[17] - 多线程概念(2)
  11. ReactiveCocoa应用篇(二)
  12. Detect Capital
  13. Redis学习——Linux环境下Redis的安装(一)
  14. JSP学习1---创建一个简单的jsp程序
  15. Mock测试接口
  16. springmvc 项目完整示例09 maven项目创建
  17. RPM包定制
  18. Python3学习之路~5.12 hashlib &amp; hmac &amp; md5 &amp; sha模块
  19. MVC四大筛选器—ActionFilter&amp;ResultedFilter
  20. Python-元类 单例

热门文章

  1. NOIP 2013 花匠 神仙操作
  2. springcloud 向Eureka中注册服务异常java.net.ConnectException: Connection refused: connect
  3. E20170609-ts
  4. Wannafly挑战赛29A御坂美琴
  5. [APIO2007]动物园
  6. C#基础 集合
  7. C# 代码笔记_文件
  8. RS485通信和Modbus协议(转)
  9. rem自适应布局小结001
  10. Git在工作中对项目的操作流程