题意:
给定一系列的点,然后求最多有多少个点在同一条直线上。
思路:
穷举起点和终点,然后依次判断每个点是否在这条直线上,时间复杂度n^3,水过。。。。。
注意:
一是要注意输入控制,二是题中:The outputs of two consecutive cases will be separated by a blank line.
最后一个案例之后不要输出空行,否则会WA,而不是PE。
代码如下:
<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef struct
{
int x,y;
}P;
P p[1000];
int main()
{
int i,j,n,k,ans,t;
scanf("%d",&t);
getchar(); getchar();
while(t--)
{
n=0; ans=0;
char str[100];
while(gets(str))
{
if(strlen(str)==0)
break;
sscanf(str,"%d %d",&p[n].x,&p[n].y);
n++;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
int cnt=0;
for(k=0;k<n;k++)
{
int m1=(p[j].y-p[i].y)*(p[k].x-p[j].x);
int m2=(p[k].y-p[j].y)*(p[j].x-p[i].x);
if(m1==m2)
cnt++;
}
ans=max(ans,cnt);
}
printf("%d\n",ans);
if(t)
printf("\n");
}
return 0;</span>