Rectangles HDU - 2056(求两个矩形的面积交)

本文介绍了一种通过分别对横纵坐标进行排序来处理几何问题的算法。使用C++实现,通过对坐标点进行结构化存储,并定义比较函数,实现了对输入坐标的快速排序。此方法适用于需要对点集进行预处理的几何计算场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对x与y分别排序就可以

#include <iostream>
#include <algorithm>

using namespace std;


struct node
{
    int id;
    double num;
};


bool cmp(node a,node b)
{
    if(a.num==b.num) 
        return a.id<b.id;
    return a.num<b.num;
}

int main()
{
    double x1,y1,x2,y2,x3,y3,x4,y4;
    while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4)!=EOF)
    {
        node t1[4];
        node t2[4];
        t1[0].num=x1;t2[0].num=y1;
        t1[1].num=x2;t2[1].num=y2;
        t1[2].num=x3;t2[2].num=y3;
        t1[3].num=x4;t2[3].num=y4;
        t1[0].id=t2[0].id=t1[1].id=t2[1].id=1;
        t1[2].id=t2[2].id=t1[3].id=t2[3].id=2;
        sort(t1,t1+4,cmp);
        sort(t2,t2+4,cmp);
        if(t1[0].id==t1[1].id||t2[0].id==t2[1].id)
        {
            printf("0.00\n");
            continue;
        }
        else
            printf("%.2lf\n",(t1[2].num-t1[1].num)*(t2[2].num-t2[1].num));
        
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值