题解 | #密码强度等级#按逻辑写,行数太多了
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
#include <stdio.h> #include <string.h> int main() { char password[300]; scanf("%s", password); int score = 0; int len = strlen(password); //密码长度 if (len <= 4) { score += 5; } else if (len >= 5 && len <= 7) { score += 10; } else { score += 25; } int lowerCaseNum = 0, capitalNum = 0; //字母 for (int i = 0; i < len; i++) { if (password[i] >= 'a' && password[i] <= 'z') { lowerCaseNum++; } else if (password[i] >= 'A' && password[i] <= 'Z') { capitalNum++; } } if (lowerCaseNum == 0 && capitalNum == 0) { score += 0; } else if ((lowerCaseNum == 0 && capitalNum != 0) || (lowerCaseNum != 0 && capitalNum == 0)) { score += 10; } else if (lowerCaseNum != 0 && capitalNum != 0) { score += 20; } int digitNum = 0; //数字 for (int j = 0; j < len; j++) { if (password[j] >= '0' && password[j] <= '9') { digitNum++; } } if (digitNum == 0) { score += 0; } else if (digitNum == 1) { score += 10; } else if (digitNum > 1) { score += 20; } int symbolNum = 0; //符号 for (int k = 0; k < len; k++) { if ((password[k] >= '!' && password[k] <= '/') || (password[k] >= '{' && password[k] <= '~')) { //条件略多,分两个写 symbolNum++; } else if ((password[k] >= ':' && password[k] <= '@') || (password[k] >= '[' && password[k] <= '`')) { symbolNum++; } } if (symbolNum == 0){ score += 0; } else if (symbolNum == 1) { score += 10; } else if (symbolNum > 1) { score += 25; } //奖励机制 int totalLetterNum = lowerCaseNum + capitalNum; if (totalLetterNum != 0 && digitNum != 0 && symbolNum == 0) { score += 2; } else if ((lowerCaseNum == 0 || capitalNum == 0) && totalLetterNum != 0 && digitNum != 0 && symbolNum != 0) { //注意判断大小写字母数和字母总数 score += 3; } else if (lowerCaseNum != 0 && capitalNum != 0 && digitNum != 0 && symbolNum != 0) { score += 5; } //输出 if (score >= 90) { printf("VERY_SECURE\n"); } else if (score < 90 && score >= 80) { printf("SECURE\n"); } else if (score < 80 && score >= 70) { printf("VERY_STRONG\n"); } else if (score < 70 && score >= 60) { printf("STRONG\n"); } else if (score < 60 && score >= 50) { printf("AVERAGE\n"); } else if (score < 50 && score >= 25) { printf("WEAK\n"); } else if (score < 25 && score >= 0) { printf("VERY_WEAK\n"); } return 0; }