题意简述

原题LightOJ 1240,Point Segment Distance(3D)。

求三维空间里线段AB与C。

题解

我们设一个点在线段AB上移动,然后发现这个点与原来的C点的距离呈一个单峰状,于是珂以三分。

三分的时候处理一下,把A点坐标移动到(0,0,0)珂以方便操作。

代码

#include <cstdio>
#include <cmath> namespace fast_IO{
const int IN_LEN = 10000000, OUT_LEN = 10000000;
char ibuf[IN_LEN], obuf[OUT_LEN], *ih = ibuf + IN_LEN, *oh = obuf, *lastin = ibuf + IN_LEN, *lastout = obuf + OUT_LEN - 1;
inline char getchar_(){return (ih == lastin) && (lastin = (ih = ibuf) + fread(ibuf, 1, IN_LEN, stdin), ih == lastin) ? EOF : *ih++;}
inline void putchar_(const char x){if(oh == lastout) fwrite(obuf, 1, oh - obuf, stdout), oh = obuf; *oh ++= x;}
inline void flush(){fwrite(obuf, 1, oh - obuf, stdout);}
int read(){
int x = 0; int zf = 1; char ch = ' ';
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar_();
if (ch == '-') zf = -1, ch = getchar_();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar_(); return x * zf;
}
void write(int x){
if (x < 0) putchar_('-'), x = -x;
if (x > 9) write(x / 10);
putchar_(x % 10 + '0');
}
} using namespace fast_IO; struct Pos{
double x, y, z;
} pos[3]; long double cacDis(Pos &a, Pos &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));
} long double cac(long double num){
Pos _new = (Pos){pos[1].x * num, pos[1].y * num, pos[1].z * num};
Pos mid = (Pos){(_new.x + pos[0].x) / 2, (_new.y + pos[0].y) / 2, (_new.z + pos[0].z) / 2};
return cacDis(mid, pos[2]);
} int main(){
int t = read();
for (int j = 1; j <= t; ++j){
pos[0].x = read(), pos[0].y = read(), pos[0].z = read();
pos[1].x = read(), pos[1].y = read(), pos[1].z = read( );
pos[1].x -= pos[0].x, pos[1].y -= pos[0].y, pos[1].z -= pos[0].z;
pos[2].x = read(), pos[2].y = read(), pos[2].z = read();
pos[2].x -= pos[0].x, pos[2].y -= pos[0].y, pos[2].z -= pos[0].z;
pos[0].x -= pos[0].x, pos[0].y -= pos[0].y, pos[0].z -= pos[0].z;
long double l = 0, r = 2.0, mid;
long double mmid; long double ans = 0.0;
for (int i = 1; i <= 100; ++i){
mid = (l + r) / 2;
mmid = (mid + r) / 2;
if (cac(mid) <= cac(mmid))
r = mmid, ans = mmid;
else
l = mid;
}
printf("Case %d: %.9Lf\n", j, cac(ans));
}
return 0;
}

最新文章

  1. 推荐15款制作 SVG 动画的 JavaScript 库
  2. 构造函数,const char*与c_str
  3. Windows 部署 Redis 群集
  4. JSP公用COMMON文件
  5. hdu 5693 朋友 博弈
  6. 深入浅出TCP/IP簇
  7. python知识:json格式文本;异常处理;字符串处理;unicode类型和str类型转换
  8. linux脚本^M: bad interpreter:解决方法
  9. 启动程序的同时传参给接收程序(XE8+WIN764)
  10. h.264 fast,1/2,1/4像素运动估计与插值处理
  11. Set vs. Set&lt;?&gt;(转)
  12. jQuery中的.height()、.innerHeight()和.outerHeight()
  13. UVa 374 - Big Mod
  14. 为什么要使用TypeScript开发Web应用程序
  15. VB中Winsock连续发送出现接收不到的异常问题解决方法
  16. redis学习-string常用命令
  17. 删除CSDN点击“阅读更多”按钮跳转到登录界面的功能
  18. python自动化开发-[第十四天]-javascript(续)
  19. python3+selenium入门04-元素定位
  20. 构建基于Suricata+Splunk的IDS入侵检测系统

热门文章

  1. 【Linux开发】arm-linux-gnueabihf-gcc下载
  2. 第三次实验报告&amp;&amp;学习总结
  3. HTTP/2 最新漏洞,直指 Kubernetes!
  4. 【Luogu P2201】【JZOJ 3922】数列编辑器
  5. 题解 AT1357 【n^p mod m】
  6. OI那些事——AFO
  7. http-proxy-middleware
  8. Vue.nextTick浅析
  9. 【已解决】Error running &#39;xxx项目&#39; Command line is too long(idea版)
  10. Rust学习笔记1