您的位置:首页 > 其它

Enum:Game of Lines(POJ 3668)

2016-01-25 22:35 477 查看
              


                  画直线

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

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

#include <iostream>
#include <functional>
#include <algorithm>
#include <set>

using namespace std;

static struct _p_set
{
long double x, y;
}points[201];
set<long double>lines;

int gcd(const int, const int);

int main(void)
{
int point_sum, cut;
long double tmp;
//pair<int,int>tmp;
while (~scanf("%d", &point_sum))
{
lines.clear();
for (int i = 0; i < point_sum; i++)
scanf("%lf%lf", &points[i].x, &points[i].y);
for (int i = 0; i < point_sum; i++)
{
for (int j = i + 1; j < point_sum; j++)
{
if ((points[i].x - points[j].x) != 0)
tmp = (points[i].y - points[j].y) / (points[i].x - points[j].x);
else
tmp = (long double)INT_MAX;
lines.insert(tmp);
}
}
printf("%d\n", lines.size());
}

return EXIT_SUCCESS;
}


  


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

#include <iostream>
#include <functional>
#include <algorithm>
#include <set>

using namespace std;

static struct _p_set
{
int x, y;
}points[201];
set<pair<int,int>>lines;

int gcd(const int, const int);

int main(void)
{
int point_sum, cut;
pair<int,int>tmp;
while (~scanf("%d", &point_sum))
{
lines.clear();
for (int i = 0; i < point_sum; i++)
scanf("%d%d", &points[i].x, &points[i].y);
for (int i = 0; i < point_sum; i++)
{
for (int j = i + 1; j < point_sum; j++)
{
cut = gcd(points[i].y - points[j].y, points[i].x - points[j].x);
tmp.first = (points[i].y - points[j].y) / cut;
tmp.second = (points[i].x - points[j].x) / cut;
lines.insert(tmp);
}
}
printf("%d\n", lines.size());
}
return EXIT_SUCCESS;
}

int gcd(const int a, const int b)
{
if (b == 0)
return a;
return gcd(b, a%b);
}


  


  其实时间差不多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: