Interesting questions: Drawing (c ++ implementation) and interesting drawing
Description: on a paper that defines the Cartesian coordinate system, draw a rectangle (x1, y1) to (x2, y2) That ranges the abscissa from x1 to x2, colors the area from y1 to y2. An example of drawing two rectangles is given. The first rectangle is (1, 1) to (4, 4), expressed in green and purple. The second rectangle is (2, 3) to (6, 5), represented in blue and purple. In the figure, a total of 15 units of area are colored, and the purple part is painted twice, but the area is calculated only once. In the actual coloring process, all rectangles are painted in a uniform color. Different colors are shown in the figure for convenience.
Show all the rectangles to be painted. How many units of area are painted.
Scale and conventions of evaluation cases
1 <= n <= 100, 0 <= X and Y <=
Input:
The first line of the input contains an integer n, indicating the number of rectangles to be drawn.
In the next n rows, each line has four non-negative integers, indicating the abscissa and ordinate in the lower left corner of the rectangle to be drawn, and the abscissa and ordinate in the upper right corner.
Output: an integer indicating the number of units of area to be colored.
Input: 2
1 1 4 4
2 3 6 5
Output: 15
Analysis: Each unit can be regarded as a coordinate. In the coordinate chart, the coordinates in the lower left corner of each small square represent the square. In this way, the whole coordinate chart can be replaced by a two-dimensional array. If the color is colored, it is greater than 0, 0 is not stained, and the number of not 0 in the last statistical array is solved.
1 # include <iostream> 2 using namespace std; 3 void input (int x1, int y1, int x2, int y2, int a [100] [100]) // assign a value to the array (stained) 4 {5 for (int I = x1; I <x2; I ++) 6 {7 for (int j = y1; j <y2; j ++) 8 a [I] [j] ++; 9} 10} 11 12 int main () 13 {14 static int a [100] [100]; 15 int n, x1, y1, x2, y2; 16 cin> n; 17 for (int I = 0; I <n; I ++) 18 {19 cin> x1> y1> x2> y2; 20 input (x1, y1, x2, y2, a); 21} 22 n = 0; 23 // Search for numbers not 0 24 for (int I = 0; I <100; I ++) 25 {26 for (int j = 0; j <100; j ++) 27 {28 if (a [I] [j]! = 0) 29 n ++; 30} 31} 32 cout <n <endl; 33 system ("pause"); 34 return 0; 35}