zoukankan      html  css  js  c++  java
  • Enum:Game of Lines(POJ 3668)

                  

                      画直线

      题目大意:给定一些点集,要你找两点之间的连线不平行的有多少条

      数据量比较少,直接暴力枚举,然后放到set查找即可

     1 #include <iostream>
     2 #include <functional>
     3 #include <algorithm>
     4 #include <set>
     5 
     6 using namespace std;
     7 
     8 static struct _p_set
     9 {
    10     long double x, y;
    11 }points[201];
    12 set<long double>lines;
    13 
    14 int gcd(const int, const int);
    15 
    16 int main(void)
    17 {
    18     int point_sum, cut;
    19     long double tmp;
    20     //pair<int,int>tmp;
    21     while (~scanf("%d", &point_sum))
    22     {
    23         lines.clear();
    24         for (int i = 0; i < point_sum; i++)
    25             scanf("%lf%lf", &points[i].x, &points[i].y);
    26         for (int i = 0; i < point_sum; i++)
    27         {
    28             for (int j = i + 1; j < point_sum; j++)
    29             {
    30                 if ((points[i].x - points[j].x) != 0)
    31                     tmp = (points[i].y - points[j].y) / (points[i].x - points[j].x);
    32                 else
    33                     tmp = (long double)INT_MAX;
    34                 lines.insert(tmp);
    35             }
    36         }
    37         printf("%d
    ", lines.size());
    38     }
    39 
    40     return EXIT_SUCCESS;
    41 }

      

      在讨论版那里还找到了一种很新奇的做法,可以无视除数是0和精度的问题

     1 #include <iostream>
     2 #include <functional>
     3 #include <algorithm>
     4 #include <set>
     5 
     6 using namespace std;
     7 
     8 static struct _p_set
     9 {
    10     int x, y;
    11 }points[201];
    12 set<pair<int,int>>lines;
    13 
    14 int gcd(const int, const int);
    15 
    16 int main(void)
    17 {
    18     int point_sum, cut;
    19     pair<int,int>tmp;
    20     while (~scanf("%d", &point_sum))
    21     {
    22         lines.clear();
    23         for (int i = 0; i < point_sum; i++)
    24             scanf("%d%d", &points[i].x, &points[i].y);
    25         for (int i = 0; i < point_sum; i++)
    26         {
    27             for (int j = i + 1; j < point_sum; j++)
    28             {
    29                 cut = gcd(points[i].y - points[j].y, points[i].x - points[j].x);
    30                 tmp.first = (points[i].y - points[j].y) / cut;
    31                 tmp.second = (points[i].x - points[j].x) / cut;
    32                 lines.insert(tmp);
    33             }
    34         }
    35         printf("%d
    ", lines.size());
    36     }
    37     return EXIT_SUCCESS;
    38 }
    39 
    40 int gcd(const int a, const int b)
    41 {
    42     if (b == 0)
    43         return a;
    44     return gcd(b, a%b);
    45 }

      

      其实时间差不多

  • 相关阅读:
    安卓版php服务器的mysql数据库增删改查简单案例
    PHP之Mysql常用SQL语句示例的深入分析
    PHP文件上传主要代码讲解
    只能输入数字的文本框-php
    Unknown column '' in 'field list'解决方案
    PHP mysqli连接MySQL数据库
    Php连接及读取和写入mysql数据库的常用代码
    mysqli 操作数据库
    类的静态变量访问
    用JS添加文本框案例代码
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/5158936.html
Copyright © 2011-2022 走看看