安全密码(字符串)

cpp 复制代码
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool is_secure_password(const char* password);
int main()
{
    int M;
    char password[51];
    // 读取输入中的密码数量 M
    scanf("%d", &M);
    // 处理每个密码
    for (int i = 0; i < M; ++i)
    {
        // 读取密码
        scanf("%s", password);

        // 检查密码是否安全并输出结果
        if (is_secure_password(password))
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}
// 函数定义
bool is_secure_password(const char* password)
{
    // 检查密码长度
    int len = strlen(password);
    if (8 <= len && len <= 16)
    {
        // 检查密码字符类别
        bool categories[4] = { false };  // 大写字母、小写字母、数字、特殊符号
        for (int i = 0; i < len; ++i) {
            char current = password[i];
            if ('A' <= current && current <= 'Z')
            {
                categories[0] = true;
            }
            else if ('a' <= current && current <= 'z')
            {
                categories[1] = true;
            }
            else if ('0' <= current && current <= '9')
            {
                categories[2] = true;
            }
            else if (strchr("~!@#$%^&", current) != NULL)
            {
                categories[3] = true;
            }
        }
        // 判断密码是否包含至少三组字符类别
        if (categories[0] + categories[1] + categories[2] + categories[3] >= 3)
        {
            return true;
        }
    }
    return false;
}
相关推荐
geovindu16 小时前
go: Floyd-Warshall Algorithms
开发语言·后端·算法·golang
db_murphy16 小时前
机器学习决策树的基尼系数是个啥?
学习·算法
拳里剑气17 小时前
C++算法:优先级队列
开发语言·c++·算法·优先级队列
小七在进步17 小时前
数据结构:用队列实现栈
开发语言·数据结构
Turbo正则17 小时前
机器学习入门笔记 | 基础算法及其应用场景
笔记·算法·机器学习
东华万里17 小时前
第37篇 手撕二叉树与堆的底层逻辑
数据结构·算法
知无不研17 小时前
算法:判断一个数是不是2的幂
数据结构·算法
luj_17681 天前
残熵算法实时化三大瓶颈突破
c语言·开发语言·网络·经验分享·算法
mCell1 天前
你以为短链接只是 Hash + 301/302?
后端·算法·架构
码少女1 天前
数据结构——哈希表的基础
数据结构·散列表