zoukankan      html  css  js  c++  java
  • hdoj 1086 You can Solve a Geometry Problem too (判断线段相交)

    You can Solve a Geometry Problem too

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

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

    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.00
    0.00 0.00 1.00 0.00
    0
     
    Sample Output
    1
    3
     

    题意 :给你 n 条线段,求交点个数。

    解题思路:http://www.cnblogs.com/Duahanlang/archive/2013/05/11/3073434.html

    AC 代码:

    View Code
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<cmath>
     5 
     6 using namespace std;
     7 
     8 const int maxv = 105;
     9 struct point {          //
    10     double x;           // x 坐标
    11     double y;           // y 坐标
    12 } first[maxv], final[maxv];
    13 
    14 double intersect(point p1, point q1, point p2)
    15 {
    16     point tmp1, tmp2;
    17     tmp1.x = q1.x - p1.x, tmp1.y = q1.y - p1.y;     // tmp1 = q1-p1   是向量
    18     tmp2.x = p2.x - p1.x, tmp2.y = p2.y - p1.y;     // tmp2 = p2-p1   是向量
    19     double ans = tmp1.x * tmp2.y - tmp2.x * tmp1.y; // ans = (q1-p1) x (p2-p1) (向量叉积)
    20     return ans;
    21 }
    22 
    23 int main()
    24 {
    25     int n, i, j;
    26     double ans1, ans2, ans3, ans4;
    27     int cnt;                //相交次数
    28     while(cin >> n && n) {
    29         cnt = 0;
    30         memset(first, 0, sizeof(first));
    31         memset(final, 0, sizeof(final));
    32         for(i = 0; i < n; ++i) {
    33             cin >> first[i].x >> first[i].y >> final[i].x >> final[i].y;
    34         }
    35         for(i = 0; i < n; ++i) {
    36             for(j = i+1; j < n; ++j) {
    37                 ans1 = intersect(first[i], final[i], first[j]);
    38                 ans2 = intersect(first[i], final[i], final[j]);
    39                 double t1 = ans1 * ans2;
    40                 ans3 = intersect(first[j], final[j], first[i]);
    41                 ans4 = intersect(first[j], final[j], final[i]);
    42                 double t2 = ans3 * ans4;
    43                 if(t1 <= 0 && t2 <= 0)
    44                     cnt++;
    45             }
    46         }
    47         cout << cnt << endl;
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    Java中取两位小数
    MySQL的IFNULL函数
    JS计算本周一和本周五的日期
    巅峰,不在一万米的边缘
    一段遍历4X4表格,取出每个单元格内容组合成文本的JS代码
    一个用于发送HTML格式邮件的类
    在Java程序中做字符串拼接时一定要记得的MessageFormat.format
    人就这么一辈子,要做自己想做的事情
    错误: 类Something是公共的, 应在名为 Something.java 的文件中声明
    Java中,权限修饰符的权限测试
  • 原文地址:https://www.cnblogs.com/Duahanlang/p/3073443.html
Copyright © 2011-2022 走看看