题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String password = scanner.nextLine(); int len = password.length(); int numberCount = 0; int lowerLetterCount = 0; int upperLetterCount = 0; int symbolCount = 0; int difficulty = 0; for (int i = 0; i < password.length(); i++) { char c = password.charAt(i); if (c <= '9' && c >= '0') { numberCount++; } else if (c <= 'z' && c >= 'a') { lowerLetterCount++; } else if (c <= 'Z' && c >= 'A') { upperLetterCount++; } else { symbolCount++; } } difficulty += len <= 4 ? 5 : len < 8 ? 10 : 25; difficulty += lowerLetterCount > 0 && upperLetterCount > 0 ? 20 : lowerLetterCount == 0 && upperLetterCount == 0 ? 0 : 10; difficulty += numberCount > 1 ? 20 : numberCount * 10; difficulty += symbolCount > 1 ? 25 : symbolCount * 10; int reward = 0; if ((lowerLetterCount > 0 || upperLetterCount > 0) && numberCount > 0) { reward += 2; if (symbolCount > 0) { reward += 1; if (lowerLetterCount > 0 && upperLetterCount > 0) { reward += 2; } } } difficulty += reward; // System.out.println(difficulty); String[] levels = {"AVERAGE", "STRONG", "VERY_STRONG", "SECURE", "VERY_SECURE"}; String difficultyLevel = difficulty < 25 ? "VERY_WEAK" : difficulty < 50 ? "WEAK" : levels[(difficulty / 10) - 5]; System.out.println(difficultyLevel); } } }