You are given a string representing an attendance record for a student. The record only contains the following three characters:
- 'A' : Absent.
- 'L' : Late.
- '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
输入一串字符,如果A的总数大于等于2或连续出现3个或以上L,则返回false;否则返回true。
思路:手动判断前两个字符是否为A,然后从第3个字符开始用循环判断第i-2,i-1,i个字符是否为L,以及当前字符是否为A。
bool checkRecord(string s) {
int a=0,l=0,i;
if(s[0]=='A')a++;
if(s.size()>=2&&s[1]=='A')a++;
if (a>1)return false;
for(i=2;i<s.size();i++)
{
if(s[i]=='A')
{
a++;
if (a>1)return false;
}
else if(s[i-2]=='L'&&s[i-1]=='L'&&s[i]=='L')return false;
}
return true;
}

本文介绍了一种用于检查学生考勤记录是否符合奖励条件的简单算法。该算法通过检查记录中缺席次数是否超过一次及是否存在连续三次以上的迟到情况来决定学生是否能够获得奖励。
558

被折叠的 条评论
为什么被折叠?



