yuri01 题解分享 · 2025/1/30
矩形总面积(编程题) - 题解
```cpp // https://dashoj.com/d/lqbproblem/p/116 #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int x1, y1, x2, y2; int x3, y3, x4, y4; int s1, s2, overlapW, overlapH, s; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; // 输入 s1 = (x2 - x1) * (y2 - y1), s2 = (x4 - x3) * (y4 - y3); // 计算两个矩形面积 overlapW = max(0, min(x2, x4) - max(x1, x3)); // 重叠面积宽 overlapH = max(0, min(y2, y4) - max(y1, y3)); // 重叠面积高 s = s1 + s2 - overlapW * overlapH; cout << s << endl; return 0; } ```
查看全文
2 0 0 3
最后的路标 题解分享 · 2024/4/8
矩形总面积(编程题) - 题解
``` #include <bits/stdc++.h> #define objl '\n' typedef long long ll; using namespace std; //数据结构在这里定义 void solve(){ //cin cout在这里 int x1,x2,x3,x4,y1,y2,y3,y4; cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4; ll area1 = (x2 - x1) * (y2 - y1); ll area2 = (x4 - x3) * (y4 - y3); // 重叠区域的宽度 沿着 x轴垂直划线 // 重叠的起始位置是 两个区域 起始点更大的那个 // 重叠的结束位置是 两个区域 结束点更小的那个 ll w = max(0, min(x2,x4) - max(x1,x3)); // 长度同理 ll h = max(0, min(y2,y4) - max(y1,y3)); ll ans = area1 + area2 - w * h; cout << ans << endl; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t;//多组数据要cin //cin >> t; t = 1; while(t--){ solve(); } return 0;//必须加return 0 } ```
查看全文
0 0 0 6