题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
#include <stdio.h> #include <string.h> int main() { //输入输出 char str[301] = {'\0'}; scanf("%s", str); char out[7][16] = {"VERY_WEAK", "WEAK", "AVERAGE", "STRONG", "VERY_STRONG", "SECURE", "VERY_SECURE",}; //统计字符 int i = 0; int LETTER = 0; int letter = 0; int digit = 0; int punctuation = 0; while (str[i] != '\0') { if (str[i] >= 'A' && str[i] <= 'Z') { LETTER++; i++; } else if (str[i] >= 'a' && str[i] <= 'z') { letter++; i++; } else if (str[i] >= '0' && str[i] <= '9') { digit++; i++; } else { punctuation++; i++; } } //统计分数 int score = 0; //一、密码长度 int len = strlen(str); if (len >= 8) { score += 25; } else if (len >= 5 && len <= 7) { score += 10; } else { score += 5; } //二、字母 if (LETTER != 0 && letter != 0) { score += 20; } else if (LETTER != 0 || letter != 0) { score += 10; } else { score += 0; } //三、数字 if (digit > 1) { score += 20; } else if (digit == 1) { score += 10; } else { score += 0; } //四、符号 if (punctuation > 1) { score += 25; } else if (punctuation == 1) { score += 10; } else { score += 0; } //五、奖励 if (LETTER != 0 && letter != 0 && digit != 0 && punctuation != 0) { score += 5; } else if ((LETTER != 0 || letter != 0) && digit != 0 && punctuation != 0) { score += 3; } else if ((LETTER != 0 || letter != 0) && digit != 0) { score += 2; } else { score += 0; } //评分标准 int level = 0; if (score >= 25) { level++; } if (score >= 50) { level++; } if (score >= 60) { level++; } if (score >= 70) { level++; } if (score >= 80) { level++; } if (score >= 90) { level++; } printf("%s", out[level]); return 0; }