POJ3668Game of Lines(数学问题)

本文解析了Game of Lines问题,介绍了如何通过计算不同斜率来找出最多不平行直线的数量。利用C++实现,考虑了斜率不存在的情况,并提供了一个有效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Game of Lines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7046 Accepted: 2638

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
* Lines 2..N+1: Line i+1 describes lattice point i with two space-separated integers: Xi and Yi.

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;
	}
	
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值