Interesting question: Drawing (c + + implementation)

Source: Internet
Author: User

Description: On a paper that defines a Cartesian coordinate system, draw a rectangle (x1,y1) to (x2,y2) to color the area of the horizontal axis from X1 to x2, and the ordinate range from Y1 to Y2. Gives an example of a two-rectangle drawing. The first rectangle is (a) to (4, 4), expressed in green and purple. The second rectangle is (2, 3) to (6, 5), expressed in blue and purple. In the figure, a total of 15 units of the area were painted color, wherein the purple portion was painted two times, but only once when calculating the area. In the actual coloring process, all the rectangles are painted in a uniform color, the picture shows that the different colors are for illustrative convenience only.

Give all the rectangles you want to draw, how many units of the total area are painted in color.

Measuring use case size and conventions

1<=n<=100,0<= horizontal axis, ordinate <=100

Input:

The first line of the input contains an integer n that represents the number of rectangles to draw.

The next n lines, 4 nonnegative integers per line, represent the horizontal and vertical coordinates of the lower-left corner of the rectangle to be drawn, and the horizontal and vertical coordinates of the upper-right corner.

Output: Outputs an integer that indicates how many units of the area have been painted in color.

Input:2

1 1 4 4

    2 3 6 5
Output:15
Analysis: Each unit can be regarded as a coordinate, we use the coordinates of each small square in the lower left corner of the coordinates to represent the square, so that the entire coordinate chart can be replaced by a two-dimensional array, if the color is more than 0, not dyed 0, the last statistical array is not 0 of the number is resolved.
1#include <iostream>2 using namespacestd;3 voidInputintX1,intY1,intX2,intY2,inta[ -][ -])//Assigning a value to an array (dyeing)4 {5      for(inti = x1; I < X2; i++)6     {7          for(intj = y1; J < Y2; J + +)8a[i][j]++;9     }Ten } One  A intMain () - { -     Static inta[ -][ -]; the     intN, x1, y1, x2, y2; -CIN >>N; -      for(inti =0; I < n; i++) -     { +CIN >> x1 >> y1 >> x2 >>Y2; - input (x1, y1, x2, Y2, a); +     } An =0; at     //retrieves a number that is not 0 -      for(inti =0; I < -; i++) -     { -          for(intj =0; J < -; J + +) -         { -             if(A[i][j]! =0) inn++; -         } to     } +cout << N <<Endl; -System"Pause"); the     return 0; *}


Interesting question: Drawing (c + + implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.