我是靠谱客的博主 谨慎羽毛,最近开发中收集的这篇文章主要介绍You can Solve a Geometry Problem too You can Solve a Geometry Problem too,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
You can Solve a Geometry Problem too
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 3
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
Author
lcy
思路:排斥试验 和 跨立试验
代码如下:
#include <iostream>
#include <cstdio>
using namespace std;
typedef struct
{
double x , y;
}point;
typedef struct
{
point p1 , p2;
}line;
double xmulti(point p1 , line l1)
{
return (p1.x-l1.p1.x)*(l1.p2.y-l1.p1.y) - (l1.p2.x-l1.p1.x)*(p1.y-l1.p1.y);
}
bool inter(line l1 , line l2)
{
if(min(l1.p1.x , l1.p2.x) <= max(l2.p1.x , l2.p2.x)&&
min(l2.p1.x , l2.p2.x) <= max(l1.p1.x , l1.p2.x)&&
min(l1.p1.y , l1.p2.y) <= max(l2.p1.y , l2.p2.y)&&
min(l2.p1.y , l2.p2.y) <= max(l1.p1.y , l1.p2.y)&&
xmulti(l1.p1 , l2)*xmulti(l1.p2,l2) <= 0&&
xmulti(l2.p1 , l1)*xmulti(l2.p2 ,l1)<=0)
return true;
else
return false;
}
int main()
{
int n;
line l[105];
while(scanf("%d",&n),n)
{
for(int i = 0; i < n; i ++)
{
scanf("%lf%lf%lf%lf",&l[i].p1.x,&l[i].p1.y,&l[i].p2.x,&l[i].p2.y);
}
int c = 0;
for(int i = 0; i < n; i ++)
{
for(int j = i+1; j < n; j ++)
{
if(inter(l[i] , l[j]))
c ++;
}
}
printf("%dn",c);
}
return 0 ;
}
最后
以上就是谨慎羽毛为你收集整理的You can Solve a Geometry Problem too You can Solve a Geometry Problem too的全部内容,希望文章能够帮你解决You can Solve a Geometry Problem too You can Solve a Geometry Problem too所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复