畫圖2014-09
阿新 • • 發佈:2019-01-31
問題描述 :
在一個定義了直角座標系的紙上,畫一個(x1,y1)到(x2,y2)的矩形指將橫座標範圍從 x1到 x2,縱座標範圍從 y1到 y2之間的區域塗上顏色。
下圖給出了一個畫了兩個矩形的例子。第一個矩形是(1,1) 到(4, 4),用綠色和紫色表示。第二個矩形是(2, 3)到(6, 5),用藍色和紫色表示。圖中,一共有 15個單位的面積被塗上顏色,其中紫色部分被塗了兩次,但在計算面積時只計算一次。在實際的塗色過程中,所有的矩形都塗成統一的顏色,圖中顯示不同顏色僅為說明方
在一個定義了直角座標系的紙上,畫一個(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
樣例輸出
15
評測用例規模與約定
1<=n<=100,0<=橫座標、縱座標<=100。
下面給出參考程式碼:
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub int sum = 0; int[][] a = new int[101][101]; Scanner sin=new Scanner(System.in); int num=sin.nextInt(); List<regtangle> myList=new ArrayList<regtangle>(); for(int i=0;i<num;i++){ myList.add(new regtangle(sin.nextInt(),sin.nextInt(),sin.nextInt(),sin.nextInt())); } for(int i=0;i<num;i++){ for(int j=myList.get(i).x1;j<myList.get(i).x2;j++){ for(int k=myList.get(i).y1;k<myList.get(i).y2;k++){ a[j][k]=1; } } } for(int i=0;i<101;i++){ for(int j=0;j<101;j++){ if(a[i][j]==1){ sum++; } } } System.out.println(sum); } } class regtangle{ int x1,y1,x2,y2; public regtangle(int x1, int y1, int x2, int y2) { super(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } 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; } }