Menu
CoddyTech

回顾:座位表

本课属于 Coddy R 学习旅程的“逻辑与流程”单元。第 21 课,共 64 课。

challenge icon

挑战

中等

完成 seat_report(seats)。seats 是一个由 0(空座位)和 1(已占用座位)组成的矩阵,每行对应剧院的一排座位。返回:

  • 每排一行,Row 1: 2 free
  • Most free: row 后接空座位最多的那一排(如果并列,则选第一排)
  • Occupied: 后接已占用座位的百分比,四舍五入到整数,再接 %

提供的代码会从一行输入中读取矩阵:各行用 ; 分隔,每行中的值用 , 分隔,因此 1,2;3,4 表示一个矩阵,其中两行为 1 2 和 3 4。代码会打印返回的每一行。

亲手试试

seat_report <- function(seats) {
  # 在这里编写你的代码
  character(0)
}

# 提供的输入和输出代码:保持原样
input <- suppressWarnings(readLines(file("stdin")))
parse_matrix <- function(line) {
  rows <- strsplit(line, ";")[[1]]
  matrix(as.numeric(unlist(strsplit(rows, ","))), nrow = length(rows), byrow = TRUE)
}
cat(seat_report(parse_matrix(input[1])), sep = "\n")

逻辑与流程中的所有课

自己练习: R 在线编译器