热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HDU1086.YoucanSolveaGeometryProblemtoo【判断两线段相交】【数学题】【12月30】

YoucanSolveaGeometryProblemtooProblemDescriptionManygeometry(几何)problemsweredesignedinth

You can Solve a Geometry Problem too

Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :)
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.

Note:
You can assume that two segments would not intersect at more than one point. 
 
Input Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending. 
A test case starting with 0 terminates the input and this test case is not to be processed.
 
Output For each case, print the number of intersections, and one line one case.
 
Sample Input
2
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.00
3
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.000
0.00 0.00 1.00 0.00
0
 
Sample Output
1
3

一开始还控制精度···额,发现并不用~~只要处理好什么时候相交就可以了~~代码如下:

#include
#include
#include
using namespace std;
const int maxn = 110;
struct ss
{
double x1, x2, y1, y2;
double k;
bool flag;
};
bool check(ss a, ss b)
{
//无斜率平行
if(a.flag == b.flag && a.flag == false) return false;

//有一直线无斜率
if(a.flag == false || b.flag == false)
{
//a无斜率
if(a.flag == false)
{
double y = b.k*a.x1 + b.y1 -b.k*b.x1;
if(y max(b.y1, b.y2)) return false;
else return true;
}
else//b无斜率
{
double y = a.k*b.x1 + a.y1 -a.k*a.x1;
if(y max(a.y1, a.y2)) return false;
else return true;
}
}
//有斜率平行
if(a.k == b.k) return false;
else//不平行
{
double x = (b.y1-b.k*b.x1-a.y1+a.k*a.x1)/(a.k-b.k);
if(x max(a.x1, a.x2) || x max(b.x1, b.x2)) return false;
else return true;
}
}
int N, ans;
int main()
{
while(scanf("%d", &N) != EOF && N)
{
ss f[maxn];
ans = 0;
for(int i = 0;i {
scanf("%lf %lf %lf %lf", &f[i].x1, &f[i].y1, &f[i].x2, &f[i].y2);
if(f[i].x1 > f[i].x2)
{
swap(f[i].x1, f[i].x2);
swap(f[i].y1, f[i].y2);
}
if(f[i].x1 == f[i].x2) f[i].flag = false;//无斜率
else
{
f[i].flag = true;//有斜率
f[i].k = (f[i].y2 - f[i].y1)/(f[i].x2 - f[i].x1);
}
}
for(int i = 1;i for(int j = 0;j {
if(check(f[i], f[j])) ans ++;
}
cout < }
return 0;
}



推荐阅读
  • 本文详细探讨了一道涉及算法、C++及图论知识点的题目,适合对算法竞赛感兴趣的读者。通过分析题目【这是一道大水题】,我们将探索如何高效地处理区间查询与更新问题。本文由技术作者【ღCauchyོꦿ࿐】撰写,旨在帮助读者掌握相关技术和解题技巧。 ... [详细]
  • 深入探讨栈和队列的应用实例——铁轨问题(Rails, ACM/ICPC CERC 1997, UVa 514)。该问题设定在一个城市火车站,涉及n节车厢从A方向驶入车站,并需按照特定顺序驶出B方向的铁轨。本文将通过算法实现来验证特定顺序的可行性。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 主板IO用W83627THG,用VC如何取得CPU温度,系统温度,CPU风扇转速,VBat的电压. ... [详细]
  • 本文探讨了如何通过预处理器开关选择不同的类实现,并解决在特定情况下遇到的链接器错误。 ... [详细]
  • 本文探讨了一段包含基类与派生类的C++代码,重点分析了虚函数的调用机制及其对程序行为的影响。代码示例包括了两个类的定义:Base和Derived,以及它们之间的继承关系。 ... [详细]
  • Imreadingthisdocument:http:software.intel.comen-usarticlesinteractive-ray-tracing我正在阅读这个文 ... [详细]
  • 本文介绍了一个C++程序,该程序用于计算一个向量首尾索引的和。当向量长度为偶数时,程序会遇到对称对,如v1[0] + v1[last]与v1[last] + v1[0],这些实际上是相同的计算结果,因此需要排除重复项以提高效率。 ... [详细]
  • 首先说一下,这是我在CSDN上的第一个文章,其实这个账号早在几年前就申请了,不过当时只是为了下载一个资源,而且也不怎么懂信息技术相关的领域,后来就再也没怎么动过,直到今天我才开始使用这个账号 ... [详细]
  • 探讨了一个关于Windows C++开发中遇到的乱码问题,特别是在处理宽字符时出现的情况。本文通过一个具体的示例——一个简单的窗口应用程序,展示了如何正确地使用宽字符以避免乱码。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
author-avatar
赖-哥_528
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有