我是靠谱客的博主 谨慎羽毛,这篇文章主要介绍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
复制代码
1
2
3
4
5
6
7
8
9
10
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
2
3
4
1 3
 

Author
lcy
 
思路:排斥试验 和 跨立试验

代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(101)

评论列表共有 0 条评论

立即
投稿
返回
顶部