Game of Lines 的解答优快云

本文详细介绍了解决GameofLines问题的方法,通过双重循环找出所有不平行直线,使用二维数组记录每种平行线的斜率,特别注意分子分母为0的情况。最终,通过化简斜率并存储在数组中,计算出Bessie在游戏中的最大得分。

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

Game of Lines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7330 Accepted: 2752
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

USACO 2008 February Silver

该题比较简单,就是用 双重循环把有多少条不平行的直线就行,开一个二维数组,储存每种平行线的斜率,化简斜率,用二维数组分别表示分子,分母,其中要注意分子分母为0的情况,下面是我AC的代码

   **

#include <iostream>
        #include <algorithm>
        #include <cstdio>
        char flag[4050][2050];
        struct point{
        	int x,y;
        };
        point p[205];
        int g(int x,int y)//求最大公约数
        {
        	if(x<y){
        		int t=x;
        		x=y;
        		y=t;
        	}
            int r;
            while(y>0)
            {
              r=x%y;
              x=y;
              y=r;
            }
        	return x;
        }
        int main(){
        	
        	
        		int n;
        		int countn=0;
        		scanf("%d",&n);
        		for(int i=0;i<n;i++){
        			scanf("%d%d",&p[i].x,&p[i].y);
        		}
        		for(int i=0;i<n-1;i++){
        			for(int j=i+1;j<n;j++){
        				int x=p[j].x-p[i].x;
        				int y=p[j].y-p[i].y;
        				if(x==0){
        					if(flag[0][1]!='1'){
        						countn++;
        						flag[0][1]='1';
        				}
        				}
        				else if(y==0){
        					if(flag[1][0]!='1'){
        						countn++;
        						flag[1][0]='1';}
        				}
        				else{
        					int k;
        					if(x>0&&y<0){
        						x=-x;
        						y=-y;
        						k=g(-x,y);
        					}
        					else if(x<0&&y<0){
        						x=-x;
        						y=-y;
        						k=g(x,y);
        					}
        					else if(x<0&&y>0){
        						k=g(-x,y);
        					}
        					else k=g(x,y);
        					x=x/k+2002;
        					y=y/k+2;
        					if(flag[x][y]!='1'){
                                countn++;
                                flag[x][y]='1';
        					}
        				}
        				
        			}
        		}
        		printf("%d\n",countn);
        	return 0;
        }

**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值