题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

#include <iostream>
#include <string>
using namespace std;

//密码类
class Passward {
  private:
    string s;

  public:
    //构造函数
    Passward(const string s) {
        this->s = s;
        return;
    }

    //析构函数
    ~Passward() {}

    //获取密码长度
    int getlen(void)const {
        return this->s.size();
    }

    //判断是否有大写字母
    bool hasCapitalLetter(void)const {
        //遍历字符串
        for (char ch : this->s)
            if (('A' <= ch) && (ch <= 'Z'))
                return true;
        return false;
    }

    //判断是否有小写字母
    bool hasLowercaseLetter(void)const {
        for (char ch : this->s)
            if (('a' <= ch) && (ch <= 'z'))
                return true;
        return false;
    }

    //判断是否有数字
    bool hasNumber(void)const {
        for (char ch : this->s)
            if (('0' <= ch) && (ch <= '9'))
                return true;
        return false;
    }

    //判断是否有其他字符
    bool hasOtherChar(void)const {
        for (char ch : this->s) {
            if ((ch == ' ') || (ch == '\n'))
                continue;
            if (ch < '0')
                return true;
            if (('9' < ch) && (ch < 'A'))
                return true;
            if (('Z' < ch) && (ch < 'a'))
                return true;
            if (ch > 'z')
                return true;
        }
        return false;
    }

    //获取大小写字母、数字和其他字符的种类数
    int getNumofKinds(void)const {
        int num = 0;
        if (this->hasCapitalLetter())
            num++;
        if (this->hasLowercaseLetter())
            num++;
        if (this->hasNumber())
            num++;
        if (this->hasOtherChar())
            num++;
        return num;
    }

    //由于含有长度超过3的重复子串的字符串必含有长度为3的重复子串,只需判断是否有长度为3的重复子串

    //是否有长度为3的重复子串
    bool hasRepeatedSubstring(void)const {
        int len = this->getlen();
        for (int i = 0; i < len - 6; i++) {
            string s1 = this->s.substr(i, 3);
            for (int j = i + 3; j < len - 3; j++) {
                string s2 = this->s.substr(j, 3);
                if (s1 == s2)
                    return true;
            }
        }
        return false;
    }

    //判断密码是否符合要求
    bool judge(void) {
        if (this->getlen() <= 8)
            return false;
        if (this->getNumofKinds() < 3)
            return false;
        if (this->hasRepeatedSubstring())
            return false;
        return true;
    }
};

int main() {
    string s;
    while (cin >> s) { // 注意 while 处理多个 case
        Passward p(s);
        if (p.judge())
            cout << "OK" << endl;
        else
            cout << "NG" << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

06-06 03:40
已编辑
电子科技大学 Java
在秋招的小白菜很想养修勾:一眼 苍穹外卖+谷粒商城,项目换一换吧,可以找一些付费知识星球博主带带,避免烂大街。多投投大厂,背背八股,你这学历乱杀了,等实习经验到位,到时候大厂闭眼选
投递美团等公司7个岗位
点赞 评论 收藏
分享
头像
05-16 11:16
已编辑
东华理工大学 Java
牛客737698141号:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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