题解 | #密码验证合格程序#
密码验证合格程序
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")