buaa 1033 Easy Problem(三分)(简单)
Easy Problem
时间限制:1000 ms | 内存限制:65536 KB
描写叙述
In this problem, you're to calculate the distance between a point P(xp, yp, zp) and a segment (x1, y1, z1) ? (x2, y2, z2), in a 3D space, i.e. the minimal distance from P to any point Q(xq, yq, zq) on the segment (a segment is part of a line).
输入
The first line contains a single integer T (1 ≤ T ≤ 1000), the number of test cases. Each test case is a single line containing 9 integers xp, yp, zp, x1, y1, z1, x2, y2, z2. These integers are all in [-1000,1000].
输出
For each test case, print the case number and the minimal distance, to two decimal places.
例子输入
3
0 0 0 0 1 0 1 1 0
1 0 0 1 0 1 1 1 0
-1 -1 -1 0 1 0 -1 0 -1
例子输出
Case 1: 1.00
Case 2: 0.71
Case 3: 1.00
题意:
为在一条线段上找到一点,与给定的P点距离最小。
非常明显的凸性函数,用三分法来解决。dist函数即为求某点到P点的距离。注意精度问题。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#define eps 1e-8
using namespace std; typedef struct node
{
double x,y,z;
}node;
node l,r,p; double dist(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
} int sgn(double a)
{
return (a>eps)-(a<-eps);
} node getmid(node a,node b)
{
node mid;
mid.x=(a.x+b.x)/2;
mid.y=(a.y+b.y)/2;
mid.z=(a.z+b.z)/2;
return mid;
} node search()
{
node mid,midmid;
while(sgn(dist(l,r))>0)
{
mid=getmid(l,r);
midmid=getmid(mid,r);
if(dist(p,mid)<dist(p,midmid))
r=midmid;
else
l=mid;
}
return r;
} int main()
{
int t;node k;
cin>>t;
for(int i=1;i<=t;i++)
{
cin>>p.x>>p.y>>p.z;
cin>>l.x>>l.y>>l.z;
cin>>r.x>>r.y>>r.z;
k=search();
printf("Case %d: %.2lf\n",i,dist(k,p));
}
return 0;
}
buaa 1033 Easy Problem(三分)(简单)的更多相关文章
- D. Easy Problem(简单DP)
题目链接:http://codeforces.com/contest/1096/problem/D 题目大意:给你一个字符串,然后再给你去掉每个字符串的每个字符的花费,然后问你使得字符中不再存在har ...
- POJ 2826 An Easy Problem!(简单数论)
Description Have you heard the fact "The base of every normal number system is 10" ? Of co ...
- 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)
An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- 杭电oj An easy problem
</pre><h1 style="color: rgb(26, 92, 200);">An easy problem</h1><stron ...
- CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu?
CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu? Description (原题来自刘汝佳<训练指南>Pa ...
- UVA-11991 Easy Problem from Rujia Liu?
Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...
- An easy problem
An easy problem Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)
Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...
- POJ 2826 An Easy Problem?!
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7837 Accepted: 1145 ...
随机推荐
- [转]自适应网页设计(Responsive Web Design)
本文转自:http://www.ruanyifeng.com/blog/2012/05/responsive_web_design.html 作者: 阮一峰 日期: 2012年5月 1日 随着3G的普 ...
- DAO模式详解
DAO模式 数据访问层(DAO): 数据的增.删.改.查操作: 业务逻辑层(service): 业务来往的操作,需要调用数据访问层则调用数据访问层,传递数据: 表现层(UI): 呈现数据,用户交互. ...
- 1B课程笔记分享_StudyJams_2017
课程1B 概述 课程1B主要讲解了Android UI的ViewGroups(视图组).LinearLayout(线性布局).RelativeLayout(相对布局),Portrait Mode(竖屏 ...
- JS——思维拓展
1.阶乘求和:4的阶乘是1*2*3*4 <script> function jiechen(value) { var n = 1; for (var i = 1; i <= valu ...
- GitHub代码托管平台搭建
GitHub代码托管平台搭建 注册账户以及创建仓库 要想使用github第一步当然是注册github账号了, github官网地址:https://github.com/. 之后就可以创建仓库了(免费 ...
- Python 之pdb调试
# 调试方式一:python -m pdb test.py # 相关命令: # l 查看当前代码 # n 向下执行一行代码 # c continue继续执行代码 # b break + 行数 添加断点 ...
- Spring处理自动装配的歧义性
1.标识首选的bean 2.使用限定符@Qualifier 首先在bean的声明上添加@Qualifier 注解: @Component @Qualifier("cdtest") ...
- 记录--git命令行上传项目到github仓库
由于公司一直使用的是的SVN,基本上都是内网,原来的git命令都快忘记了,当然也是自己太懒,平时都是直接拖到github上.今天打开idea后突然看到了原来自己写好的一个项目,就想将它上传到githu ...
- [C++] 配平化学方程式算法的封装
有人已经实现了配平的方法,在此不再重复介绍. https://www.cnblogs.com/Elfish/p/7631603.html 但是,上述的方法所提供的代码还是存在着问题,需要进一步修改. ...
- POJ3278——Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 114140 Accepted: 35715 ...