551. Student Attendance Record I

本文介绍了一种用于检查学生考勤记录是否符合奖励条件的简单算法。该算法通过扫描字符串来判断是否存在超过一次的缺席('A')或者连续三次以上的迟到('L'),并据此决定学生是否有资格获得奖励。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are given a string representing an attendance record for a student. The record only contains the following three characters:

  1. 'A' : Absent. 
  2. 'L' : Late.
  3. 'P' : Present. 

A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late)

You need to return whether the student could be rewarded according to his attendance record.

Example 1:

Input: "PPALLP"
Output: True

Example 2:

Input: "PPALLL"
Output: False
根据题意,遇到2个以上的‘A’和连续的3个‘L’,返回false,否则返回true。代码如下:

public class Solution {
    public boolean checkRecord(String s) {
        int countL = 0, countA = 0;
        for (char ch: s.toCharArray()) {
            if (ch == 'L') {
                countL ++;
                if (countL >= 3) {
                    return false;
                }
            } else if (ch == 'A') {
                countA ++;
                countL = 0;
                if (countA >= 2) {
                    return false;
                }
            } else {
                countL = 0;
            }
        }
        return true;
    }
}

报错1146意味着你在MySQL中尝试访问一个不存在的表,即`school_db.attendance_records`。根据之前的数据库结构描述,确实没有提到`attendance_records`这个表。如果你想要追踪学生出勤情况,按照之前提供的信息,这应该是在`classes`表中关联到`students`表的记录。然而,如果你想有一个专门的出勤记录表,那么你可能需要先创建这样一个表,例如添加字段如`class_id`, `student_id`, `attendance_date`, 和 `status` 等来记录每次的出勤情况。 以下是创建`attendance_records`表的一个示例: ```sql CREATE TABLE attendance_records ( record_id INT AUTO_INCREMENT PRIMARY KEY, class_id INT, student_id INT, attendance_date DATE, is_present BOOLEAN DEFAULT 1, -- 1 表示出席,0表示缺席 FOREIGN KEY (class_id) REFERENCES classes(class_id), FOREIGN KEY (student_id) REFERENCES students(student_id) ) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; ``` 一旦有了这个表,你可以通过这个新表来统计出勤率或者其他出勤相关的指标。如果你现在想查看某个老师的所有课程及其学生的出勤情况,可以做跨表连接查询: ```sql SELECT c.course_id, c.course_name, s.student_name, COUNT(ar.is_present) AS attendance_count FROM courses c JOIN classes cl ON c.course_id = cl.course_id JOIN students s ON cl.student_id = s.student_id LEFT JOIN attendance_records ar ON cl.class_id = ar.class_id AND s.student_id = ar.student_id WHERE c.teacher_id = <your_teacher_id> GROUP BY c.course_id, c.course_name, s.student_name; ``` 这里 `<your_teacher_id>` 需替换为你关注的具体教师ID。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值