zoukankan      html  css  js  c++  java
  • No.223 Rectangle Area

    No.223 Rectangle Area

    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.


      求两个矩形相交的面积
      垂直矩形,每个矩形由左下角和右上角两个点的坐标表示
      其实就是求区间段[B,D]和[F,H]相交区域 * [A,C]和[E,G]相交区域(×)
      题意理解有问题:是求两个矩形表示的区域的面积,而不是求其交集!!!

      这么简单一问题都出错了!!

     1 #include "stdafx.h"
     2 #include <map>
     3 #include <vector>
     4 #include <iostream>
     5 using namespace std;
     6 
     7 class Solution
     8 {
     9 public:
    10     int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
    11     {//求两个矩形相交的面积
    12      //垂直矩形,每个矩形由左下角和右上角两个点的坐标表示
    13      //其实就是求区间段[B,D]和[F,H]相交区域 * [A,C]和[E,G]相交区域(×)
    14      //题意理解有问题:是求两个矩形表示的区域的面积,而不是求其交集!!!
    15         int area = (C-A)*(D-B) + (G-E)*(H-F);
    16         if(F>=D || B>=H || E>=C || A>=G)//不相交
    17             return area;
    18         //只考虑到相交,没考虑到包含!!!
    19         int length = min(D,H) - max(B,F);//式子也算错!!!
    20         int width = min(C,G) - max(A,E);
    21         
    22         return area - length*width;
    23     }
    24 };
    25 
    26 int main()
    27 {
    28     Solution sol;
    29     
    30     cout << sol.computeArea(0,0,0,0,-1,-1,1,1) <<endl;
    31     cout << sol.computeArea(-3,0,3,4,0,-1,9,2) <<endl;    
    32     cout << sol.computeArea(0,-1,9,2,-3,0,3,4) <<endl;    
    33     cout << sol.computeArea(-2,1,-1,3,0,1,1,3) <<endl;
    34 
    35 }
  • 相关阅读:
    清除 Windows 系统垃圾的 bat
    java通过System.getProperty获取系统属性
    【转】Angular之constructor和ngOnInit差异及适用场景
    p中不能包含div
    How to make PostgreSQL functions atomic?
    How to chain a command after sudo su?
    javascript柯里化
    如何在Angular优雅编写HTTP请求
    angular default project (angular.json的解读)
    error:crosses initialization of ...的解决办法
  • 原文地址:https://www.cnblogs.com/dreamrun/p/4571177.html
Copyright © 2011-2022 走看看