Scala 实现
题目描述:
输入描述:
输出描述:
示例
输入:
- 2
- present
- present present
输出:
true true
输入:
- 2
- present
- present absent present present leaveearly present absent
输出:
- true false
代码:
def main(args: Array[String]): Unit = {
// 获取输入
val num = StdIn.readLine().toInt
// 记录输出
var res = new StringBuffer()
// 针对每条出勤进行判断
for (i <- 1 to num) {
// 用于跳出循环
Breaks.breakable {
// 获取出勤信息
val info = StdIn.readLine().split(" ").toList
// 1.缺勤不超过1次
val absentNum = info.toStream.count(_.equals("absent"))
if (absentNum > 1) {
res.append("false ")
Breaks.break() // 跳出循环
}
// 2.没有连续的迟到/早退
for (j <- 0 to info.length - 2) {
var cur = info(j)
var next = info(j + 1)
// 判断连续出现的情况
if (("late".equals(cur) | "leaveearly".equals(cur)) & ("late".equals(next) | "leaveearly".equals(next))) {
res.append("false ")
Breaks.break() // 跳出循环
}
}
// 3.任意连续7次考勤 缺勤/迟到/早退 不超过3次
for (j <- 0 to info.length - 8) {
// 获取到子串
var subInfo = new ListBuffer[String]
for (k <- j to j + 7) {
subInfo += info(k)
}
// 遍历子串,判断总次数
var a, b, c = 0
for (elem <- subInfo) {
elem match {
case "absent" => a += 1
case "late" => b += 1
case "leaveearly" => c += 1
}
}
if (a + b + c > 3) res.append("false ")
Breaks.break() // 跳出循环
}
// 其他则为获得出勤奖的情况
res.append("true ")
}
}
// 输出结果
println(res)
}