考勤

 * 公司用一个字符串来标识员工的出勤信息
 *      absent:    缺勤
 *      late:      迟到
 *      leaveEarly:早退
 *      present:   正常上班
 * 现需根据员工出勤信息,判断本次是否能获得出勤奖,
 *  能获得出勤奖的条件如下:
 *      1.缺勤不超过1次
 *      2.没有连续的迟到/早退
 *      3.任意连续7次考勤 缺勤/迟到/早退 不超过3次
 * 输入描述:
 *      用户的考勤数据字符串记录条数  >=1
 *      输入字符串长度 <10000 ;
 *      不存在非法输入
 * 如:
 *      2
 *      present
 *      present absent present present leaveEarly present absent
 * 输出描述:
 *      根据考勤数据字符串
 *      如果能得到考勤奖输出true否则输出false
//缺勤
    public static final String ABSENT = "absent";
    //迟到
    public static final String LATE = "late";
    //早退
    public static final String LEAVE_EARLY = "leaveEarly";
    //正常
    public static final String PRESENT = "present";

    /**
     *
     * @param items 记录的条数
     * @param list 记录
     * @return 是否有考勤奖
     */
    public List<Boolean> answer(int items, ArrayList<String> list){
        if (items != list.size()){
            throw new IllegalArgumentException("输入的记录条数不匹配");
        }
        List<Boolean> booleanList = new ArrayList<>();
        for (String str : list){
            String[] split = str.split(" ");
            boolean b = absentOverOnce(split);
            if (!b){
                booleanList.add(b);
                continue;
            }
            boolean b1 = continueLateAndLeaveEarly(split);
            if (!b1){
                booleanList.add(b1);
                continue;
            }
            boolean b2 = overThree(split);
            booleanList.add(b2);
        }
        return booleanList;
    }
    /*
    1.缺勤不超过1次
     */
    public boolean absentOverOnce(String[] str){
        int num = 0;
        for (String s : str) {
            if (s.equals(ABSENT)) {
                num++;
            }
            if (num > 1)
                return false;
        }
        return true;
    }
    /*
    2.没有连续的迟到/早退
     */
    public boolean continueLateAndLeaveEarly(String[] str){
        for (int i = 0; i < str.length; i++) {
            if (str[i].equals(LATE) || str[i].equals(LEAVE_EARLY)){
                i++;
                if (i < str.length && (str[i].equals(LATE) || str[i].equals(LEAVE_EARLY))){
                    return false;
                }
            }
        }
        return true;
    }


    /*
        3.任意连续7次考勤 缺勤/迟到/早退 不超过3次
     */
    public boolean overThree(String[] str){
        int num;
        for (int i = 0; i < str.length; i++) {
            num = 0;
            if (str[i].equals(LATE) || str[i].equals(LEAVE_EARLY) || str[i].equals(ABSENT)){
                num = 1;
                int j;
                for (j = i + 1; j < i + 7 && j < str.length; j++) {
                    if (str[j].equals(LATE) || str[j].equals(LEAVE_EARLY) || str[j].equals(ABSENT)){
                        num++;
                    }

                    if (num > 3){
                        return false;
                    }
                }
                if (j == str.length){
                    return true;
                }
            }
        }
        return true;
    }

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务