题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream> using namespace std; #include<string> #include<cctype> int main() { string str; int type; int typeCnt[4]; string word; string str2; bool flag; while (getline(cin, str)) { flag = true; type = 0; for (int i = 0; i < 4; i++) { typeCnt[i] = 0; } if (str.length() <= 8) { cout << "NG" << endl; flag = false; } for (int i = 0; i < str.length(); i++) { if (islower(str[i])) { typeCnt[0] = 1; } if (isupper(str[i])) { typeCnt[1] = 1; } if (isdigit(str[i])) { typeCnt[2] = 1; } if (isgraph(str[i]) && !isalpha(str[i]) && !isdigit(str[i])) { typeCnt[3] = 1; } } for (int i = 0; i < 4; i++) { type = type + typeCnt[i]; } if (type < 3) { cout << "NG" << endl; flag = false; } for (int i = 0; (i <= str.length() - 3) && flag; i++) { word.assign(str, i, 3); str2.assign(str, i + 3, str.length() - (i + 3)); if (str2.find(word) != -1) { cout << "NG" << endl; flag = false; } } if (flag) { cout << "OK" << endl; } } } // 64 位输出请用 printf("%lld")
使用cctype头文件进行字符类型判断,用find进行重复字符串查找,从前往后找就行
isdigit() //判断是否为数字 isalpha() //判断是否为字母 isalnum() // 判断是否为字母或者数字 iscntrl() // 判断是否为控制字符 isgraph() // 判断是否为除空格外的打印字符 islower() // 判断是否为小写字符 isprint() // 判断是否为打印字符 ispunct() // 判断是否为标点符号 isspace() // 判断是否为空格 isupper() // 判断是否为大写字母 isxdigit() // 如果参数是十六进制数字,0~9、a~f、A~F,返回true tolower() // 如果参数是大写字符,返回其小写 toupper() // 如果参数是小写字符,返回其大写
华为机试刷题记录 文章被收录于专栏
记录一下手打代码的解题思路方便复习