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 }
  • 相关阅读:
    selenium入门:基于界面的自动化测试
    selenium入门:环境搭建
    Python入门:迭代器
    Python入门:生成器并行(协程)
    Python入门:生成器应用-斐波拉契数列
    Python入门:生成器定义
    Python入门:装饰器案例3
    Python入门:装饰器案例2
    Python入门:装饰器案例1
    Spring MVC 上传文件---依赖引用
  • 原文地址:https://www.cnblogs.com/dreamrun/p/4571177.html
Copyright © 2011-2022 走看看