zoukankan      html  css  js  c++  java
  • hdu 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): 10204    Accepted Submission(s): 5042

    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
    1.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
    判断两个线段有没有交点  百度  叉积
    /*
    判断AB和CD两线段是否有交点:
    同时满足两个条件:('x'表示叉积)
    1.C点D点分别在AB的两侧.(向量(ABxAC)*(ABxAD)<=0)
    2.A点和B点分别在CD两侧.(向量(CDxCA)*(CDxCB)<=0)
    */
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 using namespace std;
     6 struct node{
     7     double x,y;
     8 }a[105],b[105];
     9 double chaji(node a,node b,node c){
    10     return  (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
    11 }
    12 int judge(node a,node b,node c,node d){
    13     if(max(a.x,b.x)<min(c.x,d.x)||max(c.x,d.x)<min(a.x,b.x))
    14         return 0;
    15     if(max(a.y,b.y)<min(c.y,d.y)||min(a.y,b.y)>max(c.y,d.y))
    16         return 0;
    17     if(chaji(a,c,d)*chaji(b,c,d)<=0&&(chaji(c,a,b)*chaji(d,a,b)<=0))
    18        return 1;
    19     //if(chaji(c,d,a,b)<=0||chaji(c,d,b,a)<=0)
    20      //   return 1;
    21     //if(chaji(d,c,a,b)<=0||chaji(d,c,b,a)<=0)
    22       //  return 1;
    23     return 0;
    24 }
    25 int main(){
    26     int t;
    27     int i,j;
    28     while(scanf("%d",&t)!=EOF){
    29         if(t==0)
    30             break;
    31         for(i=1;i<=t;i++){
    32             scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&b[i].x,&b[i].y);
    33         }
    34         int ans=0;
    35         for(i=1;i<=t;i++){
    36             for(j=i+1;j<=t;j++){
    37                 if(judge(a[i],b[i],a[j],b[j]))
    38                 {
    39                     ans++;
    40                     //cout<<judge(a[i],b[i],a[j],b[j])<<endl;
    41                 }
    42             }
    43         }
    44         printf("%d
    ",ans);
    45     }
    46 
    47 }
  • 相关阅读:
    SGU 187 Twist and whirl
    伸展树---初步学习
    poj 2503 Babelfish
    sublime 3 phpfmt配置(大括号对齐)
    Linux Shell 错误: $' ': command not found错误解决
    redis 使用场景
    wireshake tcp 三次握手详解
    ip地址和子网掩码
    phpstorm 远程调式 php
    win10,ubuntu时间不对问题
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/5947726.html
Copyright © 2011-2022 走看看