题解 | 密码强度等级
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import java.util.*;
import java.lang.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static int lenJudge(String pwd){
int len = pwd.length();
if (len<=4) return 5;
else if (len<=7) return 10;
else return 25;
}
public static int charJudge(String pwd){
StringBuffer sb_small = new StringBuffer();
StringBuffer sb_big= new StringBuffer();
for(int i=0;i<26;i++){
sb_small.append((char)(65+i));
sb_big.append((char)(97+i));
}
String sSmall = sb_small.toString(), sBig = sb_big.toString();
boolean has_small = false, has_big = false;
for(int i=0;i<pwd.length();i++){
if(sSmall.contains(pwd.substring(i,i+1))) has_small = true;
if(sBig.contains(pwd.substring(i,i+1))) has_big = true;
}
if(has_big && has_small) return 20;
else if (has_big || has_small) return 10;
else return 0;
}
public static int numJudge(String pwd){
String nums = "0123456789";
int cnt = 0;
for(int i=0;i<pwd.length();i++){
if(nums.contains(pwd.substring(i,i+1))) cnt+=1;
}
if(cnt<=0) return 0;
else if (cnt==1) return 10;
else return 20;
}
public static int fJudge(String pwd){
String cList = "!\"#$%&'()*+,-./:;<=>?@:;<=>?@[\\]^_`{|}~";
int cnt = 0;
for(int i=0;i<pwd.length();i++){
if(cList.contains(pwd.substring(i,i+1))) cnt+=1;
}
if(cnt<=0) return 0;
else if(cnt==1) return 10;
else return 25;
}
public static String finalRes(int score){
if(score>=90) return "VERY_SECURE";
else if (score>=80) return "SECURE";
else if (score>=70) return "VERY_STRONG";
else if (score>=60) return "STRONG";
else if (score>=50) return "AVERAGE";
else if (score>=25) return "WEAK";
else return "VERY_WEAK";
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String pwd = in.nextLine();
int score = 0;
score+=lenJudge(pwd);
int charScore = charJudge(pwd);
score+=charScore; //*
int numScore = numJudge(pwd);
score+=numScore; //*
int fScore = fJudge(pwd);
score+=fScore; //*
if(charScore==20 && numScore>0 && fScore>0) score+=5;
else if (charScore==10 && numScore>0 && fScore>0) score+=3;
else if (charScore>0 && numScore>0 && fScore==0) score+=2;
// System.out.println(score);
System.out.println(finalRes(score));
}
}
}
模拟题,纯粹靠细心
