题目描述:
-
公司用一个字符串来标识员工的出勤信息。
- absent: 缺勤
- late: 迟到
- leaveearly:早退
- present: 正常上班
-
现需根据员工出勤信息,判断本次是否能获得出勤奖。 -
能获得出勤奖的条件如下:
- 缺勤不超过1次
- 没有连续的迟到/早退
- 任意连续7次考勤 缺勤/迟到/早退 不超过3次
输入描述:
- 用户的考勤数据字符串记录条数 >=1,输入字符串长度 <10000,不存在非法输入。
输出描述:
- 根据考勤数据字符串,如果能得到考勤奖输出true否则输出false。
示例:
输入:
输出:
输入:
- 2
- present present
- absent present late present present present present leaveearly late present present
输出:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class Award {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = canAward(n);
System.out.println(s);
}
public static String canAward(int n) {
Scanner sc = new Scanner(System.in);
ArrayList<List<String>> persons = new ArrayList<>();
for (int i = 0; i < n; ++i) {
String[] days = sc.nextLine().split(" ");
List<String> list = Arrays.stream(days).collect(Collectors.toList());
persons.add(list);
}
sc.close();
StringBuilder sb = new StringBuilder();
for (List<String> person : persons) {
long absent = person.stream()
.filter(x -> x.equals("absent")).count();
if (absent > 1) {
sb.append("false").append(" ");
continue;
}
boolean flag = true;
for (int i = 0; i < person.size() - 1; ++i) {
String cur = person.get(i);
String next = person.get(i + 1);
if (("late".equals(cur) || "leaveearly".equals(cur))
&& ("late".equals(next) || "leaveearly".equals(next))) {
flag = false;
break;
}
}
if (!flag) {
sb.append(flag).append(" ");
continue;
}
for (int i = 0; i < person.size() - 7; ++i) {
List<String> days = new ArrayList<>();
for (int j = 0; j < 7; ++j) {
String day = person.get(i + j);
days.add(day);
}
long absentCount = days.stream().filter(x -> x.equals("absent")).count();
long lateCount = days.stream().filter(x -> x.equals("late")).count();
long leaveEarlyCount = days.stream().filter(x -> x.equals("leaveearly")).count();
long count = absentCount + lateCount + leaveEarlyCount;
if (count > 3) {
flag = false;
break;
}
}
if (!flag) {
sb.append(flag).append(" ");
continue;
} else {
sb.append(flag).append(" ");
}
}
return sb.toString();
}
}
|