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

密码验证合格程序

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

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

// 1. 得到输入
// 2. 遍历判断字符总类
// 3. 判断重复子串
// 4. 输出
int main() {
    
    char input[110] = {0};

    _Bool low = 0;
    _Bool upp = 0;
    _Bool digit = 0;
    _Bool other = 0;
    int len = 0;

    int first = 0;
    int slow = 0;
    _Bool repeat = 0;

    while(scanf("%s", input) == 1)
    {
        if(strlen(input) <= 8)
        {
            puts("NG");
            continue;
        }

        len = strlen(input);
        low = upp = digit = other = 0;
        for(int i = 0; i < len; ++ i)
        {
            if(islower(input[i]))
            {
                low = 1;
            }
            else if(isupper(input[i]))
            {
                upp = 1;
            }
            else if(isdigit(input[i]))
            {
                digit = 1;
            }
            else
            {
                other = 1;
            }
        }

        if((low+upp+digit+other) < 3)
        {
            puts("NG");
            continue;
        }


        //只需要判断有没有长度为3的子串重复即可
        //因为长度大于3的子串如果有重复那么肯定会有长度为3的子串重复
        //快慢指针,slow指针遍历完结束
        repeat = 0;
        for(slow = 0; slow < len-3; ++ slow)
        {
            if(repeat)
            {
                puts("NG");
                break;
            }

            for(first = slow + 3; first < len; ++ first)
            {
                if(input[slow]==input[first] && input[slow+1]==input[first+1] && input[slow+2]==input[first+2])
                {
                    repeat = 1;
                    break;
                }
            }
        }



        if(!repeat)
        {
            puts("OK");
        }
        

    }

    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务