uva 270 Lining Up

题意:问你若干个点中最多有几个在同一条直线上,那么我们先确定一条直线,然后再一一找就好了,在同一直线的条件是斜率相同,我们把它转化成等式的形式,就能避免误差了,至于怎么选点的方法就跟组合一个意思
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int  MAXN = 750 ;

struct node
{
	int x,y;
}arr[MAXN];
int nIndex;
char str[1000];

void input()
{
	nIndex = 0 ;
	while (gets(str))   //gets()将'\n'变成 '\0'
	{
		if (!str[0])
			break;
		sscanf(str,"%d%d",&arr[nIndex].x,&arr[nIndex].y);
		nIndex++;
	}
}

bool is_in_line(int x1,int y1,int x2,int y2,int x3,int y3)
{
	return ((x1-x2)*(y3-y2)-(x3-x2)*(y1-y2))==0;
}

void solve()
{
	int nmax = 2; 
	for (int i = 0 ; i < nIndex; i++)
	{
		for (int j = i+1 ; j < nIndex; j++)
		{
			int cnt = 2;
			for (int k = j+1 ; k < nIndex; k++)
			{
				if (is_in_line(arr[i].x,arr[i].y,arr[j].x,arr[j].y,arr[k].x,arr[k].y) )
						cnt++;
			}
			if (cnt > nmax)
				nmax = cnt;
		}
	}
	printf("%d\n",nmax);
}	

int main()
{
	int T;
	scanf("%d",&T);
	getchar();
	gets(str);
	while (T--)
	{
		input();
		if (nIndex == 1)
			printf("1\n");
		else if ( nIndex == 2 )
			printf("2\n");
		else solve();
		if (T)	
			printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值