编程介的小学生 2020-02-09 14:33 采纳率: 0.4%
浏览 107

Colourful Rectangle 颜色长方形

Problem Description
We use Red, Green and Blue to make new colours. See the picture below:

Now give you n rectangles, the colour of them is red or green or blue. You have calculate the area of 7 different colour. (Note: A region may be covered by same colour several times, but it’s final colour depends on the kinds of different colour)

Input
The first line is an integer T(T <= 10), the number of test cases. The first line of each case contains a integer n (0 < n <= 10000), the number of rectangles. Then n lines follows. Each line start with a letter C(R means Red, G means Green, B means Blue) and four integers x1, y1, x2, y2(0 <= x1 < x2 < 10^9, 0 <= y1 < y2 < 10^9), the left-bottom's coordinate and the right-top's coordinate of a rectangle.

Output
For each case, output a line "Case a:", a is the case number starting from 1,then 7 lines, each line contain a integer, the area of each colour. (Note: You should print the areas as the order: R, G, B, RG, RB, GB, RGB).

Sample Input
3
2
R 0 0 2 2
G 1 1 3 3
3
R 0 0 4 4
G 2 0 6 4
B 0 2 6 6
3
G 2 0 3 8
G 1 0 6 1
B 4 2 7 7

Sample Output
Case 1:
3
3
0
1
0
0
0
Case 2:
4
4
12
4
4
4
4
Case 3:
0
12
15
0
0
0
0

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-25 23:42
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # 读取数据
    input_file <- "color_rectangle.txt"
    data <- readLines(input_file)
    n_cases <- as.integer(gsub("[^0-9]", "", data[1]))
    cases <- rep(1:n_cases, each = 7)
    
    for (i in 1:n_cases) {
      # 获取输入的数据
      input_data <- data[(i+1):((i+1)+7)]
      
      # 将输入的坐标转换为二维数组
      coordinates <- matrix(data=input_data, nrow=2, ncol=2)
      
      # 计算每个颜色区域的面积
      area_red <- sum(coordinates[,1] > 0 & coordinates[,2] == 0 & coordinates[,3] == 0)
      area_green <- sum(coordinates[,1] == 0 & coordinates[,2] > 0 & coordinates[,3] == 0)
      area_blue <- sum(coordinates[,1] == 0 & coordinates[,2] == 0 & coordinates[,3] > 0)
      
      # 输出结果
      cat("Case", i+1, "\n")
      cat(format(area_red, digits=2), "\n")
      cat(format(area_green, digits=2), "\n")
      cat(format(area_blue, digits=2), "\n")
    }
    

    这个程序首先读取color_rectangle.txt文件中的数据,并将其分割成不同的行。然后,它将这些行存储在名为coordinates的二维数组中。

    接下来,程序计算了每个颜色区域的面积:红色、绿色和蓝色。最后,程序输出每个颜色区域的面积,按照顺序输出(即,按红、绿、蓝的顺序)。

    评论

报告相同问题?