nyoj952——最大四边形

描述
平面坐标上有n个点,你知道能组成四边形中面积最大的是多少吗?

输入
有多组测试数据
第一行整数n,表示有n个点,( 4<=n<=300 )
然后n行,每行x,y表示点的坐标。(没有重复的点)
输出
最大四边形的面积.(保留六位小数)
样例输入

5
0 0
0 4
4 0
4 4
2 3

样例输出

16.000000


思路:枚举两个点作为一条边,然后枚举其他点,找到这条边两边最大的三角形就可以了。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int maxn=305;
const double eps=1e-8;
struct Point
{
    double x,y;
}p[maxn];

double muti_cross(Point a,Point b,Point c)//叉乘求面积
{
    double x1=a.x-b.x;
    double x2=a.x-c.x;
    double y1=a.y-b.y;
    double y2=a.y-c.y;
    return (x1*y2-x2*y1)*0.5;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        double area=0,lmax,rmax;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){//i,j两点作为一条边
                lmax=0;
                rmax=0;
                for(int k=0;k<n;k++){//枚举其他点
                    if(i!=k&&j!=k)
                    {
                        double s=muti_cross(p[k],p[i],p[j]);
                        lmax=max(lmax,-s);//点可能在边的左侧或者右侧
                        rmax=max(rmax,s);
                    }
                }
                if(lmax<=eps||rmax<=eps)//排除三角形的情况
                    continue;
                area=max(area,lmax+rmax);
            }
        }
        printf("%0.6f\n",area);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值