蓝桥杯水题 矩形面积交

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AC_jie/article/details/79626375

一开始以为是扫描线算法,但是仔细一看基础题出扫描线?,想了一下一直卡在两个矩形的左右关系上,看了题解才发现,原来矩形的面积交与坐标的相对位置有关,其实就是位置第二大的x与位置第三大的x可以组成矩形,y同理.

#include<cstdio>
#include<iostream>
#include<iomanip>
using namespace std;
struct Node
{
    double x;
    double y;
};
int main()
{
    Node a,b,c,d;
    double x1,y1,x2,y2;
    while(cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y >> d.x >> d.y)
    {
        x1 = min(max(a.x,b.x),max(c.x,d.x)); //左上角x
        y1 = min(max(a.y,b.y),max(c.y,d.y)); //左上角y
        x2 = max(min(a.x,b.x),min(c.x,d.x));
        y2 = max(min(a.y,b.y),min(c.y,d.y));
        if(x1 > x2 && y1 > y2)
        {
            cout << fixed << setprecision(2) << (x1 - x2) * (y1 - y2) << endl;
        }
        else
            cout << "0.00" << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/AC_jie/article/details/79626375