HDU4082Hou Yi's secret(相似三角形的个数)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4082


题意:

给定n个点判断这些点能组成的三角形中,与同一个三角形相似的三角形的最大个数。


分析:

就是暴力枚举。但是写的时候要注意以下几点。

1)必须得能组成三角形。

2)去掉重复的点。。。被坑了,orz


代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const double eps = 1e-8;

const int maxn = 20;

struct point{
    double x,y;
}p[maxn];

struct triangle{
    double a[3];
}t[maxn*maxn*maxn];

int cross(point p0,point p1,point p2){
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dis(point p1,point p2){
    return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}

bool similar(triangle t1,triangle t2){
    if( (fabs(t1.a[0]*t2.a[1]-t1.a[1]*t2.a[0])<eps)&&
        (fabs(t1.a[1]*t2.a[2]-t1.a[2]*t2.a[1])<eps)&&
        (fabs(t1.a[0]*t2.a[2]-t1.a[2]*t2.a[0])<eps))
    return true;
    return false;
}

int main()
{
    int n;
    while(~scanf("%d",&n)&&n){
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&p[i].x,&p[i].y);
            for(int j=0;j<i;j++){
                if(p[i].x==p[j].x&&p[i].y==p[j].y){
                    i--;n--;
                    break;
                }
            }
        }
        int cnt = 0;
        if(n<3){
            puts("0");
            continue;
        }
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                for(int k=j+1;k<n;k++){
                    if(cross(p[i],p[j],p[k])){
                        t[cnt].a[0]=dis(p[i],p[j]);
                        t[cnt].a[1]=dis(p[i],p[k]);
                        t[cnt].a[2]=dis(p[j],p[k]);
                        sort(t[cnt].a,t[cnt].a+3);
                        cnt++;
                    }
                }
            }
        }
        int ans = 0;
        for(int i=0;i<cnt;i++){
            int tmp = 1;
            for(int j=i+1;j<cnt;j++)
                if(similar(t[i],t[j]))
                    tmp++;
            ans=max(ans,tmp);
        }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值