题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4082
给你若干个点,求相似三角形最大数量。点最多只有18个,可以尽情暴力。
WA点在于:
- 点有重复,需要判重。
- 开方(sqrt)损失了精度。
敲黑板 谨记谨记:以后计算几何的题尽量少开方!!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<math.h>
using namespace std;
int num[3000];
int cnt;
struct point
{
double x,y;
}m[20];
struct hh
{
double x1,x2,x3;
}m1[8000],m2;
bool dengyu(struct hh k1,struct hh k2)
{
if((k1.x1*k2.x2==k1.x2*k2.x1)&&(k1.x1*k2.x3==k1.x3*k2.x1)&&(k1.x2*k2.x3==k1.x3*k2.x2))
return true;
return false;
}
void sort1(double &n1,double &n2,double &n3)///从小到大排序
{
double n4;
if(n1>n2)
{n4=n1;
n1=n2; n2=n4;
}if(n1>n3)
{
n4=n1;n1=n3;n3=n4;
}if(n2>n3)
{
n4=n2;n2=n3;n3=n4;
}
}
void jiao(struct point A,struct point B,struct point C)
{
double a,b,c;
a=((B.x-C.x)*(B.x-C.x)+(B.y-C.y)*(B.y-C.y));
b=((A.x-C.x)*(A.x-C.x)+(A.y-C.y)*(A.y-C.y));
c=((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
sort1(a,b,c);
m1[cnt].x1=a; m1[cnt].x2=b; m1[cnt].x3=c;
cnt++;
}
int main()
{
int nn,t1,t2,n;
while(scanf("%d",&nn)!=EOF)
{
if(nn==0)
break;
cnt=0;
bool vis[205][205];
memset(vis,0,sizeof vis);
n=0;
for(int i = 0; i <nn; i++)
{
scanf("%d%d",&t1,&t2);
if(!vis[t1+100][t2+100])
{
m[n].x = double(t1);
m[n].y = double(t2);
n++;
vis[t1+100][t2+100]= true;
}
}
for(int i=0;i<n-2;i++)
{
for(int j=i+1;j<n-1;j++)
{
for(int k=j+1;k<n;k++)
{
if((m[i].x-m[j].x)/(m[i].y-m[j].y)==(m[i].x-m[k].x)/(m[i].y-m[k].y));
else
jiao(m[i],m[j],m[k]);
}
}
}
if(cnt==0)
{
printf("0\n");
continue;
}
//sort(m1,m1+cnt,cmp);
m2=m1[0];
int ans,maxn;
// printf("%d**\n",cnt);
maxn=0;
ans=1;
///int i=1;
memset(num, 0, sizeof(num));
for(int i = 0; i < cnt; i ++)
{
for(int j = 0; j < cnt; j ++)
{
if(dengyu(m1[i],m1[j]))
num[i] ++;
}
}
for(int i = 0; i< cnt; i ++)
{
if(maxn< num[i])
maxn= num[i];
}
printf("%d\n",maxn);
}
return 0;
}