zoukankan      html  css  js  c++  java
  • 数三角

    链接:https://ac.nowcoder.com/acm/contest/3003/D
    来源:牛客网

    题目描述

    牛牛得到了一个平面,这个平面上有 n 个不重合的点,第 i 个点的坐标为 (xi,yi)(x_i,y_i)(xi,yi)。
    牛牛想知道,这 n 个点形成的三角形中,总共有多少个钝角三角形。

    输入描述:

    第一行,一个正整数 n,表示点数。
    第二行至第 n+1 行中,第 i+1 行包含两个整数 xi,yi,表示第 i 个点的坐标。
    保证 1n500,104xi,yi104,任意两点不重合。

    输出描述:

    输出一行,一个整数表示答案。

    输入

    3
    0 0
    -1145 1
    1 0

    输出

    1

    一个三角形的三边长 a,b,cc 最长 )满足 a2+b2<c2(或存在两条边向量的点积 <0 ),则该三角形为钝角三角形。

    枚举三个点判断即可,注意判断共线和不要算重。可用两边之和大于第三边判断是否为三角形。

    时间复杂度 O(n^3) 。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <sstream>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const int mod=1e9+7;
    16 const double eps =1e-8;
    17 const int maxn=1e4+10;
    18 using namespace std;
    19  
    20 struct node
    21 {
    22     LL x,y;
    23 }PT[505];
    24  
    25 LL pointDistance( LL x1, LL y1, LL x2, LL y2)//返回两点距离的平方 
    26 {
    27     LL distance = (y1-y2)*(y1-y2) + (x1-x2)*(x1-x2);
    28     return distance;
    29 }
    30  
    31 int main()
    32 {
    33     #ifdef DEBUG
    34     freopen("sample.txt","r",stdin);
    35     #endif
    36  
    37     int n;
    38     scanf("%d",&n);
    39     for(int i=1;i<=n;i++)
    40         scanf("%lld %lld",&PT[i].x,&PT[i].y);
    41     int num=0;
    42     for(int i=1;i<=n;i++)
    43     {
    44         for(int j=i+1;j<=n;j++)
    45         {
    46             for(int k=j+1;k<=n;k++)
    47             {
    48                 LL dis[3]={0};
    49                 dis[0]=pointDistance(PT[i].x,PT[i].y,PT[j].x,PT[j].y);
    50                 dis[1]=pointDistance(PT[i].x,PT[i].y,PT[k].x,PT[k].y);
    51                 dis[2]=pointDistance(PT[j].x,PT[j].y,PT[k].x,PT[k].y);
    52                 sort(dis,dis+3);
    53                 if(sqrt(dis[2])==sqrt(dis[0])+sqrt(dis[1]) ) continue;//共线,不为三角形 
    54                 if(dis[2] > dis[0]+dis[1]) num++;
    55             }
    56         }
    57     }
    58     printf("%d
    ",num);
    59      
    60     return 0;
    61 }

    -

  • 相关阅读:
    10.13 新版本go on~
    9.30 总结一下九月呗
    9.25 学习下日期加减
    9.22 迎难而上不要怂!
    9.22 Sans-serif VS Serif
    9.22 keep studying
    【LeetCode刷题】最长同值路径:妙解
    【LeetCode刷题】机器人走路最大距离:妙解
    【LeetCode刷题】不使用+-的加减法:妙解
    【LeetCode刷题】NIM游戏:妙解
  • 原文地址:https://www.cnblogs.com/jiamian/p/12271695.html
Copyright © 2011-2022 走看看