7-12 检查密码

7-12 检查密码

作者 陈越

单位 浙江大学

本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点 .,还必须既有字母也有数字。

输入格式:

输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行给出一个用户设置的密码,为不超过 80 个字符的非空字符串,以回车结束。

注意: 题目保证不存在只有小数点的输入。

输出格式:

对每个用户的密码,在一行中输出系统反馈信息,分以下5种:

  • 如果密码合法,输出Your password is wan mei.;
  • 如果密码太短,不论合法与否,都输出Your password is tai duan le.;
  • 如果密码长度合法,但存在不合法字符,则输出Your password is tai luan le.;
  • 如果密码长度合法,但只有字母没有数字,则输出Your password needs shu zi.;
  • 如果密码长度合法,但只有数字没有字母,则输出Your password needs zi mu.。

输入样例:

in 复制代码
5
123s
zheshi.wodepw
1234.5678
WanMei23333
pass*word.6

输出样例:

out 复制代码
Your password is tai duan le.
Your password needs shu zi.
Your password needs zi mu.
Your password is wan mei.
Your password is tai luan le.

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

代码

c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

bool ifValid(const string &str)
{
    regex pattern1("^[a-zA-Z0-9.]+$");
    regex pattern2("^[a-zA-Z0-9]+$");
    return regex_match(str, pattern1) || regex_match(str, pattern2);
}

bool ifOnlyLetter(const string &str)
{
    regex pattern("^[a-zA-Z.]+$");
    return regex_match(str, pattern);
}

bool ifOnlyNum(const string &str)
{
    regex pattern("^[0-9.]+$");
    return regex_match(str, pattern);
}

string justifyPassword(string password)
{
    if (password.length() < 6)
        return "Your password is tai duan le.";
    else if (!ifValid(password))
        return "Your password is tai luan le.";
    else if (ifOnlyLetter(password))
        return "Your password needs shu zi.";
    else if (ifOnlyNum(password))
        return "Your password needs zi mu.";
    else
        return "Your password is wan mei.";
}

int main()
{
    int N;
    cin >> N;
    getchar();
    while (N--)
    {
        string tempStr;
        getline(cin, tempStr);
        // cout << tempStr.length();
        cout << justifyPassword(tempStr) << endl;
    }
    return 0;
}

优化代码

c++ 复制代码
#include <bits/stdc++.h>
using namespace std;

bool ifValid(const string &password)
{
    return all_of(password.begin(), password.end(), [](char c)
                  { return isalnum(c) || c == '.'; });
}

bool ifOnlyLetter(const string &password)
{
    return all_of(password.begin(), password.end(), [](char c)
                  { return isalpha(c) || c == '.'; });
}

bool ifOnlyNum(const string &password)
{
    return all_of(password.begin(), password.end(), [](char c)
                  { return isdigit(c) || c == '.'; });
}

string justifyPassword(const string &password)
{
    if (password.length() < 6)
        return "Your password is tai duan le.";
    if (!ifValid(password))
        return "Your password is tai luan le.";
    if (ifOnlyLetter(password))
        return "Your password needs shu zi.";
    if (ifOnlyNum(password))
        return "Your password needs zi mu.";
    return "Your password is wan mei.";
}

int main()
{
    int N;
    cin >> N;
    cin.ignore();
    while (N--)
    {
        string tempStr;
        getline(cin, tempStr);
        cout << justifyPassword(tempStr) << endl;
    }
    return 0;
}
  • 不用复杂的正则表达式判断密码是否合法,而是运用了三个函数isalnum()判断是否为字母或数字,isalpha()判断是否为字母,isdigit()判断是否为数字,加上all_of,对整个字符串进行检查。
  • 使用cin.ignore()而不是getchar()来读取缓冲区的换行符
相关推荐
做cv的小昊1 小时前
【大模型算法自学笔记07】NLP基础知识(1.7 Embedding)
人工智能·笔记·算法·计算机视觉·自然语言处理·transformer·embedding
weixin_307779132 小时前
Git中获取一个目录下所有文件的最后修改人的邮箱
开发语言·git·算法
电科_银尘2 小时前
【大模型算法与工程 56】Tool Calling:工具描述、结构化参数与返回协议
jvm·算法·oracle
hai3152475433 小时前
阶乘分层关系模型存储文本与表格
数据结构·算法·rust·排序算法·线性回归
小白的高手之路3 小时前
【LeetCode 热题 100】206. 反转链表|链表、迭代、递归(Python)
笔记·python·算法·leetcode·链表·职场和发展·力扣
Regulus-Moon3 小时前
PageRank:把整个 Web 变成一条马尔可夫链——平稳分布、谱收敛与 d=0.85 的定价
算法·搜索引擎·推荐算法
凌冰_3 小时前
数据结构 - 数组(八)
数据结构·算法·矩阵
16月6日-晴4 小时前
122. 买卖股票的最佳时机 II【贪心】
算法·leetcode
Shaoshing4 小时前
LeetCode 1798. 你能构造出连续值的最大数目
java·数据结构·算法
大今野4 小时前
江苏专转本计算机大类基础理论考点:算法的概念与复杂度分析
数据结构·算法·专转本