题目:
在一个定义了直角坐标系的纸上,画一个(x1,y1)到(x2,y2)的矩形指将横坐标范围从x1到x2,纵坐标范围从y1到y2之间的区域涂上颜色。 下图给出了一个画了两个矩形的例子。第一个矩形是(1,1) 到(4, 4),用绿色和紫色表示。第二个矩形是(2, 3)到(6, 5),用蓝色和紫色表示。图中,一共有15个单位的面积被涂上颜色,其中紫色部分被涂了两次,但在计算面积时只计算一次。在实际的涂色过程中,所有的矩形都涂成统一的颜色,图中显示不同颜色仅为说明方便。
给出所有要画的矩形,请问总共有多少个单位的面积被涂上颜色。 输入格式 输入的第一行包含一个整数n,表示要画的矩形的个数。 接下来n行,每行4个非负整数,分别表示要画的矩形的左下角的横坐标与纵坐标,以及右上角的横坐标与纵坐标。 输出格式 输出一个整数,表示有多少个单位的面积被涂上颜色。 样例输入 2 1 1 4 4 2 3 6 5
此代码经测试满分通过,在此分享给大家
package com.amgji.cc1409_2;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner
in=
new Scanner(System.
in);
int n=
in.nextInt();
int[][] ban=
new int[
101][
101];
arc[] arr=
new arc[n];
for(
int i=
0;i<n;i++){
arr[i]=
new arc(
in.nextInt(),
in.nextInt(),
in.nextInt(),
in.nextInt());
}
for(
int i=
0;i<n;i++){
for(
int j=
0;j<arr[i].getHight();j++){
for(
int k=
0;k<arr[i].getWidth();k++){
ban[arr[i].getX1()-j][arr[i].getY1()+k]=
1;
}
}
}
int result=
0;
for(
int i=
0;i<
101;i++){
for(
int j=
0;j<
101;j++){
if(
1==ban[i][j]){
result++;
}
}
}
System.
out.println(result);
}
}
class arc{
private int x1;
private int y1;
private int x2;
private int y2;
private int width;
private int hight;
public int getWidth() {
return width;
}
public void setWidth(
int width) {
this.width = width;
}
public int getHight() {
return hight;
}
public void setHight(
int hight) {
this.hight = hight;
}
public arc() {
}
public int getX1() {
return x1;
}
public void setX1(
int x1) {
this.x1 = x1;
}
public int getY1() {
return y1;
}
public void setY1(
int y1) {
this.y1 = y1;
}
public int getX2() {
return x2;
}
public void setX2(
int x2) {
this.x2 = x2;
}
public int getY2() {
return y2;
}
public void setY2(
int y2) {
this.y2 = y2;
}
public arc(
int x1,
int y1,
int x2,
int y2) {
this.x1 =
100-y1;
this.y1 = x1;
this.x2 =
100-y2;
this.y2 = x2;
this.width = Math.abs(
this.y2-
this.y1);
this.hight = Math.abs(
this.x2-
this.x1);
}
}
转载请注明原文地址: https://ju.6miu.com/read-9556.html