Rectangle Area

柿子专挑软的捏,从最简单的题目开始练,现在上题目:

223. Rectangle Area

  • Total Accepted: 42894
  • Total Submissions: 139528
  • Difficulty: Easy

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Rectangle Area

Assume that the total area is never beyond the maximum possible value of int.

题目的意思是如果两个矩形不相交,则总的面积为两个矩形的面积和,如果相交,则用两个矩形的面积之和减去相交的面积;

智商太菜,这么简单的题目,我都要去百度别人的思路,主要体现在相交时候,数学太菜。上代码:

class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
{
int areasum = ((C - A)*(D - B)) + ((G - E)*(H - F));

int a1 = min(A, C);
int a2 = max(E, G);
int a3 = max(A, C);
int a4 = min(E, G);

int a5 = min(B, D);
int a6 = max(F, H);
int a7 = max(B, D);
int a8 = min(F, H);
if ((a1 >= a2) || (a3 <= a4))//不相交情况
{
return areasum;
}

if ((a5 >= a6) || (a7 <= a8))//不相交情况
{
return areasum;
}

//以下就是相交的情况
int right = min(C, G);
int left = max(A, E);
int top = min(H, D);
int bottom = max(F, B);
int commonArea = (right - left) * (top - bottom);

return areasum - commonArea;
}
};

太笨了,我要编程编到死!fuck!

原文地址:https://www.cnblogs.com/a-dreaming-dreamer/p/5710979.html