首页 技术 正文
技术 2022年11月15日
0 收藏 505 点赞 2,840 浏览 2472 个字

http://acm.hdu.edu.cn/showproblem.php?pid=1086

You can Solve a Geometry Problem too

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8861    Accepted Submission(s): 4317

Problem DescriptionMany 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.  InputInput 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. OutputFor each case, print the number of intersections, and one line one case. Sample Input2
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 题解:本题题干已经排除了两线重合的多边交于一点的情况,故直接枚举所有的边是否相交即可

 #include<cstdio>
#include<cmath>
using namespace std;
#define eps 1e-6
#define N 105
struct point{
double x , y ;
point(double x_, double y_){
x = x_;
y = y_;
}
point(){}
point operator - (const point a) const
{
return point(x-a.x,y-a.y);
}
double operator * (const point a) const
{
return x*a.y - a.x*y;
}
}; struct line{
point s , t;
}L[N]; int main()
{
int T;
while(~scanf("%d",&T),T)
{
for(int i = ;i < T ; i++)
{
scanf("%lf%lf%lf%lf",&L[i].s.x,&L[i].s.y,&L[i].t.x,&L[i].t.y);
}
int ans = ;
for(int i = ; i < T ; i++)
{
for(int j = i+ ; j < T ; j++)//j从i开始保证不会重复判断
{
// if(i==j) continue;
point A = L[i].s;
point B = L[i].t;
point C = L[j].s;
point D = L[j].t;
if((((D-C)*(A-C))*((D-C)*(B-C)))>eps) {continue;}
if((((D-A)*(B-A))*((C-A)*(B-A)))>eps) {continue;}
ans++;
}
}
printf("%d\n",ans);
}
return ;
}

也可以把他们写成函数在外面

 #include <cstdio>
#include <cmath>
using namespace std;
#define eps 1e-8
#define N 105
struct point{
double x, y;
point(){}
point(double _x, double _y) {
x = _x, y = _y;
} point operator - (point a){
return point(x-a.x, y-a.y);
} double operator * (point a){
return x*a.y - y*a.x;
}
}; struct line{
point s, t;
}L[N]; bool ck(line a, line b)
{
point A = a.s, B = a.t, C = b.s, D = b.t;
if(((C-A)*(B-A)) *((D-A)*(B-A)) > eps) return false;
if(((A-C)*(D-C)) *((B-C)*(D-C)) > eps) return false;
return true;
} int main()
{
int n;
while(~scanf("%d", &n), n)
{
for(int i = ; i < n; i++)
scanf("%lf %lf %lf %lf", &L[i].s.x, &L[i].s.y, &L[i].t.x, &L[i].t.y);
int cnt = ;
for(int i = ; i < n; i++)
for(int j = i+; j < n; j++)
cnt += ck(L[i], L[j]);
printf("%d\n", cnt);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918