Game of Lines
Description Farmer John has challenged Bessie to the following game: FJ has a board with dots marked at N (2 ≤ N ≤ 200) distinct lattice points. Dot i has the integer coordinates Xi and Yi (-1,000 ≤ Xi ≤ 1,000; -1,000 ≤Yi ≤ 1,000). Bessie can score a point in the game by picking two of the dots and drawing a straight line between them; however, she is not allowed to draw a line if she has already drawn another line that is parallel to that line. Bessie would like to know her chances of winning, so she has asked you to help find the maximum score she can obtain. Input * Line 1: A single integer: N Output * Line 1: A single integer representing the maximal number of lines Bessie can draw, no two of which are parallel. Sample Input 4 -1 1 -2 0 0 0 1 1 Sample Output 4 Source |
题目大意:给你n个点的x1,y1,然后问你有多少个不互相平行的直线
解题思路:这道题是个数学问题,运用到了斜率这个概念,要想让两条直线不互相平行,最重要的条件就是直线的斜率不相同,于是用map取存一下不同斜率出现了几次,输出的时候size()统计一下就好了,同时要注意一下斜率不存在即两点的y值相减为0的情况,这里我用ans取计算,然后暴力完判一下ans即可,比较水WA的原因就是放进map的时候要double一下不然很尴尬
#include<iostream>
#include<cstdio>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<vector>
#include <bitset>
#include<algorithm>
#include <queue>
#include<map>
using namespace std;
struct point
{
int x, y;
}p[205], pp;
int i, j, n;
map<double, int> XL;
int main()
{
cin >> n;
long long int ans = 0;
for (i = 1; i <= n; i++)
{
cin >> p[i].x >> p[i].y;
}
ans = 0;
for (i = 1; i <= n; i++)
{
for (j = i + 1; j <= n; j++)
{
pp.x = p[i].x - p[j].x;
pp.y = p[i].y - p[j].y;
if (pp.y == 0)
{
ans++;
}
else
{
XL[(double)pp.x / pp.y]++;
}
}
}
if (ans == 0)
{
cout <<XL.size()<< endl;
}
else
{
cout << XL.size() + 1 << endl;
}
}