1. Description of the problem
Find the area of two rectangles.
2. Methods and Ideas
The intersection area of two rectangles can be calculated first, and then the area is rectangular with two rectangular area and minus intersection area.
Note: Although the area does not exceed the maximum value of int, the middle edge length operation may be more than, pay attention to the processing details, otherwise easily overflow.
Class Solution {public:intComputearea (intAintBintCintDintEintFintGintH) {intUnionarea =0; Long Wlimit,hlimit; Wlimit = (long) min (c,g)-(long) max (a,e); hlimit = min (d,h)-max (b,f);if(Wlimit >=0&& Hlimit >=0{Unionarea = (min (c,g)-max (a,e)) * (min (d,h)-max (b,f)); }Else{Unionarea =0; } //printf("test:%lu\ n", Wlimit);return(C-A)*(D-b) + (G-E)*(H-F)-Unionarea; }};intMain () {solution Sol;printf("%d\ n", Sol.computearea (-3,0,3,4,0,-1,9,2));inta[8] = {-1500000001,0,-1500000000,1,1500000000,0,1500000001,1};//for(inti =0; I <8; i + +) scanf ("%d", &a[i]);printf("%d\ n", Sol.computearea (a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]));return 0;}
Leetcode 223 Rectangle Area