Run ID 作者 问题 语言 测评结果 时间 内存 代码长度 提交时间
91873 sh25_shenpy 矩形面积交 C++ 通过 0 MS 264 KB 2003 2026-06-17 20:52:37

Tests(7/7):


#include <iostream> #include <algorithm> #include <iomanip> #include <cmath> using namespace std; struct Rect { double x1, y1, x2, y2; double minX, maxX, minY, maxY; // 构造函数:输入任意两个相对顶点,自动计算边界 Rect(double x1, double y1, double x2, double y2) { this->x1 = x1; this->y1 = y1; this->x2 = x2; this->y2 = y2; minX = min(x1, x2); maxX = max(x1, x2); minY = min(y1, y2); maxY = max(y1, y2); } }; int main() { // 优化IO操作 ios::sync_with_stdio(false); cin.tie(NULL); double x1, y1, x2, y2; double x3, y3, x4, y4; // 读取第一个矩形的两个相对顶点 if (!(cin >> x1 >> y1 >> x2 >> y2)) return 0; // 读取第二个矩形的两个相对顶点 if (!(cin >> x3 >> y3 >> x4 >> y4)) return 0; Rect r1(x1, y1, x2, y2); Rect r2(x3, y3, x4, y4); // 计算X轴方向的重叠部分 // 重叠部分的左边界是两个矩形左边界的较大值 double overlapLeft = max(r1.minX, r2.minX); // 重叠部分的右边界是两个矩形右边界的较小值 double overlapRight = min(r1.maxX, r2.maxX); // 计算Y轴方向的重叠部分 // 重叠部分的下边界是两个矩形下边界的较大值 double overlapBottom = max(r1.minY, r2.minY); // 重叠部分的上边界是两个矩形上边界的较小值 double overlapTop = min(r1.maxY, r2.maxY); // 计算重叠的宽和高 double width = overlapRight - overlapLeft; double height = overlapTop - overlapBottom; double area = 0.0; // 只有当宽和高都大于0时,才有正面积的交集 // 如果宽或高小于等于0,说明不相交或仅相切,面积为0 if (width > 0 && height > 0) { area = width * height; } // 输出结果,固定保留两位小数 cout << fixed << setprecision(2) << area << endl; return 0; }


测评信息: