# TopCoder--计算矩形的公有面积

##### topcoder.BoxUnion

      Problem Statement
NOTE: This problem statement contains an image that may not display properly if viewed outside of the applet.
Given a list of two-dimensional rectangles, compute the area of their union. For example, the union of the three rectangles shown in the figure below:
cover an area of 35 units.
The list of rectangles will be given as a String[], where each element describes one rectangle. Each String will be formatted as 4 space-separated integers with no leading zeros, giving the coordinates of the left, bottom, right, and top of the rectangle (in that order). The three rectangles shown above would be given as:
{{1 3 5 6},
{3 1 7 5},
{4 4 9 7}}
Definition
Class:
BoxUnion
Method:
area
Parameters:
String[]
Returns:
int
Method signature:
int area(String[] rectangles)
(be sure your method is public)

Constraints
-   rectangles will contain between 1 and 3 elements, inclusive.
-   Each element of rectangles will be formatted as described in the problem statement.
-   For each rectangle, the left coordinate will be less than the right coordinate and the bottom coordinate will be less than the top coordinate.
-   All coordinates will be between 0 and 20000, inclusive.
Examples
0)
{ "200 300 203 304" }
Returns: 12
A single rectangle with area 12.
1)
{ "0 0 10 10",
"20 20 30 30" }
Returns: 200
Two disjoint rectangles, each of area 100.
2)
{ "0 500 20000 501",
"500 0 501 20000" }
Returns: 39999
These two rectangles intersect at a single point.
3)
{ "4 6 18 24",
"7 2 12 19",
"0 0 100 100" }
Returns: 10000
The third rectangle completely overlaps the first two.
4)
{ "1 3 5 6",
"3 1 7 5",
"4 4 9 7" }
Returns: 35
This is the example from the problem statement.
5)
{ "0 0 20000 20000",
"0 0 20000 20000",
"0 0 20000 20000" }
Returns: 400000000
public class BoxUnion{    int[][] intArray = new int[7][5];    int area = 0;

public int area(String[] rectangles)
{
fillArray(rectangles);
if (rectangles.length == 1)
{
this.area = intArray[0][4];
}
else
if (rectangles.length == 2)
{
// do two rectangles
intArray[2] = fillAChar(intArray[0], intArray[1]);
this.area = intArray[0][4] + intArray[1][4] - intArray[2][4];
}
else
if (rectangles.length == 3)
{
// do three rectangles
intArray[3] = fillAChar(intArray[0], intArray[1]);
intArray[4] = fillAChar(intArray[0], intArray[2]);
intArray[5] = fillAChar(intArray[1], intArray[2]);
intArray[6] = fillAChar(intArray[3], intArray[4]);
this.area = intArray[0][4] + intArray[1][4] + intArray[2][4] - intArray[3][4] - intArray[4][4]
- intArray[5][4] + intArray[6][4];
}
return this.area;
}

int[] fillAChar(int[] a, int[] b)
{
int[] c = new int[5];
c[0] = (a[0] > b[0]) ? a[0] : b[0];
c[1] = (a[1] > b[1]) ? a[1] : b[1];
c[2] = (a[2] < b[2]) ? a[2] : b[2];
c[3] = (a[3] < b[3]) ? a[3] : b[3];
if ((c[3] < c[1]) || c[2] < c[0])
{
c[4] = 0;
}
else
{
c[4] = (c[3] - c[1]) * (c[2] - c[0]);
}
return c;
}

void fillArray(String[] str)
{
int i;
for (i = str.length - 1; i >= 0; i--)
{
// System.out.println(str[i] + "/n");
int j = 0;
int k = 0;
int counter = 0;
while (counter < 3)
{
k = j;
j = str[i].indexOf(" ", k);
Integer it = new Integer(str[i].substring(k, j));
this.intArray[i][counter] = it.intValue();
counter++;
j++;
}
Integer it = new Integer(str[i].substring(j));
this.intArray[i][counter] = it.intValue();

// 判断输入的数是否非法，非法的话打印出来。
if (intArray[i][2] < intArray[i][0] || intArray[i][3] < intArray[i][0])
{
System.out.println("Input numbers ERROR:");
}
else
if (intArray[i][0] < 0 || intArray[i][1] < 0 || intArray[i][2] < 0 || intArray[i][3] < 0)
{
System.out.println("Input numbers ERROR!");
}
// 计算每个矩形的面积
intArray[i][4] = (intArray[i][2] - intArray[i][0]) * (intArray[i][3] - intArray[i][1]);
// System.out.println(intArray[i][4]);
}
}

public static void main(String[] args)
{
BoxUnion bu = new BoxUnion();

String[] str = new String[]
/*
* { "0 0 20000 20000", "0 0 20000 20000", "0 0 20000 20000" }; { "200 300 203 304" }; { "1 3 5 6", "3 1 7 5",
* "4 4 9 7" } { "0 0 10 10", "20 20 30 30" } { "0 0 20000 20000", "0 0 20000 20000", "0 0 20000 20000" } { "4 6
* 18 24", "7 2 12 19", "0 0 100 100" }
*/
{ "0 500 20000 501", "500 0 501 20000" };

System.out.println("The area is: " + bu.area(str));
}
}

• 本文已收录于以下专栏：

## TopCoder--计算矩形的公有面积2

package topcoder;public class BoxUnion2{    int[][] intArray;    int N;    public int area(String[] ...
• chouy
• 2006年06月15日 15:58
• 744

## c语言练习题 3-2 计算矩形面积

3-3 矩形面积计算 #include int max(int a,int b) { if(a>b) { return a; } else { return b; } } i...
• u013011267
• 2015年08月31日 21:46
• 842

## LeetCode 223. Rectangle Area（两个矩形的复合面积计算）

• jmspan
• 2016年04月05日 00:48
• 1125

## ZZULIOJ 1816: 矩形 【矩形重叠面积的计算（区分相离与相交）】

1816: 矩形 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 103  Solved: 32 SubmitStatusWeb Boar...
• dxx_111
• 2015年12月10日 21:11
• 1306

## C# 矩形面积 0002

using System; namespace RectangleApplication { class Rectangle { // member variables...
• yinyueai
• 2016年04月21日 03:20
• 154

## 求矩形的周长和面积【Java】

package myJava1; public class Rectangle { //定义属性，表示矩形的长和宽 int length=1; int width=1; //定义方法...
• cyanciel
• 2014年10月24日 13:55
• 1369

## 任意方位矩形相交面积计算

• cjbww
• 2017年08月09日 10:21
• 313

## 求两个矩形重叠部分的面积

#include #include #define areaFile "area.txt" #define perportionFile "perportion.txt" #define min(...
• Gone_HuiLin
• 2016年11月19日 08:16
• 1038

## 计算两个矩形重叠面积的简单方法

• DreamD1987
• 2012年06月12日 17:27
• 6291

## 如何在Java中创建一个矩形类，并通过set()传入长与宽，计算矩形的周长与面积并通过get()查看计算结果

• Bee0_0
• 2017年09月25日 16:59
• 487

举报原因： 您举报文章：TopCoder--计算矩形的公有面积 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)