题解 | #密码强度等级#

密码强度等级

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)

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

全部评论

相关推荐

爱吃的猪猪又被画饼了:问问他消息队列怎么保证消息不丢失的,消息堆积你是怎么解决的
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务