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

密码验证合格程序

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()   // 如果参数是小写字符,返回其大写
华为机试刷题记录 文章被收录于专栏

记录一下手打代码的解题思路方便复习

全部评论

相关推荐

大清早迷迷糊糊被闹钟叫醒,坐在电脑面前开始答题,硬生生坐了2小时,要是不进面,我都无颜面对我的屁股
在看数据的傻狍子很忙碌:26届还好啦。我昨晚还要跟mt值班降级熔断的测试 , 回来做一下上周的美团笔试 , 做完已经快三点了。只a出1.25。而且手机还断网了4次五六秒,已经心碎了。
投递美团等公司10个岗位 > 美团求职进展汇总
点赞 评论 收藏
分享
vip牛牛:测试吧,开发现在至少212
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务