题解 | #密码强度等级#

密码强度等级

https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361

#include <iostream>
using namespace std;
#include <string>
#include <cctype>
int lengthPoint(string str) {
    int point = 0;
    if (str.size() <= 4) {
        point = 5;
    } else if (str.size() >= 5 && str.size() <= 7) {
        point = 10;
    } else if (str.size() >= 8) {
        point = 25;
    }
    return point;
}

int numPoint(string str) {
    int point = 0;
    int cnt = 0;
    for (auto c : str) {
        if (isdigit(c)) {
            cnt++;
        }
    }
    if (cnt == 0) {
        point = 0;
    } else if (cnt == 1) {
        point = 10;
    } else if (cnt > 1) {
        point = 20;
    }
    return point;
}

int alPoint(string str) {
    int point = 0;
    int cnt = 0;
    bool hasL = false;
    bool hasU = false;
    for (auto c : str) {
        if (isalpha(c)) {
            cnt++;
            if (islower(c)) {
                hasL = true;
            } else if (isupper(c)) {
                hasU = true;
            }
        }
    }
    if (cnt == 0) {
        point = 0;
    } else {
        if (hasL && hasU) {
            point = 20;
        } else {
            point = 10;
        }
    }
    return point;
}

int otherPoint(string str) {
    int point = 0;
    int cnt = 0;
    for (auto c : str) {
        if ((c >= 0x21 && c <= 0x2F) || (c >= 0x3A && c <= 0x40) || (c >= 0x5B &&
                c <= 0x60) || (c >= 0x7B && c <= 0x7E)) {
            cnt++;
        }
    }
    if (cnt == 0) {
        point = 0;
    } else if (cnt == 1) {
        point = 10;
    } else if (cnt > 1) {
        point = 25;
    }
    return point;
}

void outPoint(int num) {
    string res;
    if (num >= 90) {
        res = "VERY_SECURE";
    } else if (num >= 80 && num < 90) {
        res = "SECURE";
    } else if (num >= 70 && num < 80) {
        res = "VERY_STRONG";
    } else if (num >= 60 && num < 70) {
        res = "STRONG";
    } else if (num >= 50 && num < 60) {
        res = "AVERAGE";
    } else if (num >= 25 && num < 50) {
        res = "WEAK";
    } else if (num >= 0 && num < 25) {
        res = "VERY_WEAK";
    }
    cout << res << endl;
}

int spPoint(string str) {
    int point = 0;
    bool hasA = false;
    bool hasN = false;
    bool hasO = false;
    bool hasL = false;
    bool hasU = false;
    for (auto c : str) {
        if (isdigit(c)) {
            hasN = true;
        } else if (isalpha(c)) {
            hasA = true;
            if (islower(c)) {
                hasL = true;
            } else if (isupper(c)) {
                hasU = true;
            }
        } else if ((c >= 0x21 && c <= 0x2F) || (c >= 0x3A && c <= 0x40) || (c >= 0x5B &&
                   c <= 0x60) || (c >= 0x7B && c <= 0x7E)) {
            hasO = true;
        }
    }
    if (hasA && hasN && !hasO) {
        point = 2;
    }
    if (hasA && hasN && hasO) {
        point = 3;
    }
    if (hasA && hasN && hasO && hasL && hasU) {
        point = 5;
    }
    return point;
}

int main() {
    string str;
    getline(cin, str);
    int num;
    num = lengthPoint(str) + numPoint(str) + alPoint(str) + otherPoint(str) + spPoint(str);
    outPoint(num);
}

全写在一个循环里也不是不行,但是又乱又不美观,条件判断还多,干脆全分开,反正一维循环时间复杂度都是O(n)

当然有优化的地方,不过过题还是条理清晰写起来比较方便比较快,就是有点困*

全部评论

相关推荐

大方的大熊猫准备进厂:1.教育背景:你希望从事什么专业的工作你的主修课就是什么;成绩优秀是你应该做的,没什么可描述的,成绩不优秀也许人家在大学忙着创业呢?(成绩优秀不一定是好事,只能说明多元化的大学你上成了高中,没有真正上明白大学,反而体现了你死板,不爱社交,没有别的突出能力) 2.实践经历:你想表达的意思没有说清楚。你是说你会个性化服务,还是你有实习经历。如果没有带来,经济收益,表彰,更好的发展前景,那你还不如说说提升了自己哪些技能。你说有人给你送锦旗我都能明白你优秀,但是你说你会xxxx,你说这话谁信,证据呢。 3.入伍经历:你描述的就是你的工作职责或者你应该做的,并没有体现出来你把这个事情做好了,而且入伍经历并不能证明你能干好你要应聘的工作,不如只写经历其余所有内容都不写。 4.荣誉技能:重点突出一下,但不要过多描述,这些荣誉的含金量懂得都懂。 重点:你要应聘什么工作(具体岗位,实习生不具体),你的期望薪资
点赞 评论 收藏
分享
风的叶脉:不知道但我想要鞭打你( '-' )ノ)`-' ) 加油
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务