POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客

下面三种情况比较特殊,特别是第三种

G++怎么交都是WA,同样的代码C++A了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const double eps = 1e-8;
const double PI = acos(-1.0);
const double INF = 1e5; int sgn(double x)
{
if(fabs(x) < eps) return 0;
return x < 0 ? -1:1;
} struct Point
{
double x,y;
Point() {}
Point(double _x,double _y)
{
x = _x,y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
//叉积
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
//点积
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
}; struct Line
{
Point p,q;
Line() {};
Line(Point _p,Point _q)
{
p = _p,q = _q;
}
//两直线相交求交点
//第一个值为0表示直线重合,为1表示平行,为2表示相交
//只有第一个值为2时,交点才有意义
pair<int,Point> operator &(const Line &b)const
{
Point res = p;
if(sgn((p-q)^(b.p-b.q)) == 0)
{
if(sgn((p-b.q)^(b.p-b.q)) == 0)
return make_pair(0,res);//重合
else return make_pair(1,res);//平行
}
double t = ((p-b.p)^(b.p-b.q))/((p-q)^(b.p-b.q));
res.x += (q.x-p.x)*t;
res.y += (q.y-p.y)*t;
return make_pair(2,res);
}
}; //*判断线段相交
bool inter(Line l1,Line l2)
{
return
max(l1.p.x,l1.q.x) >= min(l2.p.x,l2.q.x) &&
max(l2.p.x,l2.q.x) >= min(l1.p.x,l1.q.x) &&
max(l1.p.y,l1.q.y) >= min(l2.p.y,l2.q.y) &&
max(l2.p.y,l2.q.y) >= min(l1.p.y,l1.q.y) &&
sgn((l2.p-l1.q)^(l1.p-l1.q))*sgn((l2.q-l1.q)^(l1.p-l1.q)) <= 0 &&
sgn((l1.p-l2.q)^(l2.p-l2.q))*sgn((l1.q-l2.q)^(l2.p-l2.q)) <= 0;
} Point pot[105],peg;
double rad; int main()
{
// freopen("in.txt","r",stdin);
int t;
double x1,y1,x2,y2;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
Line L1=Line(Point(x1,y1),Point(x2,y2));
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
Line L2=Line(Point(x1,y1),Point(x2,y2));
if(sgn(L1.p.y - L1.q.y)==0 || sgn(L2.p.y - L2.q.y)==0)
{
puts("0.00");
continue;
}
if(!inter(L1,L2))
{
puts("0.00");
continue;
}
if(sgn(L1.p.y - L1.q.y) < 0) swap(L1.p,L1.q);
if(sgn(L2.p.y - L2.q.y) < 0) swap(L2.p,L2.q);
if(inter(Line(L1.p,Point(L1.p.x,INF)),L2))
{
puts("0.00");
continue;
}
if(inter(Line(L2.p,Point(L2.p.x,INF)),L1))
{
puts("0.00");
continue;
}
pair<int,Point> pr=L1 & L2;
Point cp=pr.second;
pr=Line(L1.p,Point(INF,L1.p.y)) & L2;
double ans=fabs((L1.p-cp)^(pr.second-cp))/2;
pr=Line(L2.p,Point(INF,L2.p.y)) & L1;
ans=min(ans,fabs((L2.p-cp)^(pr.second-cp))/2);
printf("%.2lf\n",ans);
}
return 0;
}

最新文章

  1. spring task 配置
  2. MyBatis操作指南-配置使用Provider动态生成SQL
  3. winform记事本初步实现
  4. sprint2 项目的粗略展示
  5. 查看linux僵尸进程
  6. hdu5785(极角排序求所有锐角钝角个数)
  7. poj2761Feed the dogs(划分树-区间K值)
  8. [DEncrypt] Encrypt--加密/解密/MD5加密 (转载)
  9. 11661 - Burger Time?
  10. C# yield return用法
  11. JS的作用域链与原型链
  12. CodeCraft-19 and Codeforces Round #537 Div. 2
  13. 20165237 2017-2018-2 《Java程序设计》第1周学习总结
  14. [flask]gunicorn配置文件
  15. maven——依赖管理
  16. 我是怎样和Linux系统结缘并通过红帽RHCE认证的
  17. C# abstract virtual override new finally java final finalize
  18. 天朝屁民每天做T跟菜贩一样,进菜-卖菜,为伟大的菜贩精神点赞
  19. 数据库还原错误:指定的转换无效。(SqlManagerUI)
  20. Java NIO 详解(一)

热门文章

  1. 如何使用ghost备份系统?
  2. shell进阶之tree、pstree、lsof命令详解
  3. 3d分层悬停效果
  4. XShell本地上传文件到Ubuntu上及从Ubuntu下载文件到本地
  5. 从马尔可夫模型(Markov Model)到隐马尔可夫模型(Hidden Markov Model)
  6. 【六】K8s-Pod 水平自动扩缩实践(简称HPA)
  7. uni-app 富文本解析-小程序
  8. Nacos源码结构和AP模式注册中心实现介绍
  9. 201871030136-颜静 实验三 结对项目—《D{0-1}KP 实例数据集算法实验平台》项目报告
  10. TaskManager任务管理工具类