题解 | #密码强度等级#
整型数组合并
http://www.nowcoder.com/practice/c4f11ea2c886429faf91decfaf6a310b
细心,耐心。困难题我唯唯诺诺,简单题我也写了好久才100%
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String pwd = sc.next(); isStrong(pwd); } } //示例2的密码强度为10+20+0+25+0=55分。 public static void isStrong(String str) { int alpha = checkAlpha(str); int num= checkNumber(str); int len=checkLen(str); int symbol=checkSymbol(str); int res=checkLen(str)+checkAlpha(str)+checkNumber(str)+ checkSymbol(str)+bonus_grade(str); if (res >= 90) { System.out.printf("VERY_SECURE"); } else if (res >= 80 ) { System.out.println("SECURE"); } else if (res >= 70 ) { System.out.println("VERY_STRONG"); } else if (res >= 60 ) { System.out.println("STRONG"); } else if (res >= 50 ) { System.out.println("AVERAGE"); } else if (res >= 25 ) { System.out.println("WEAK"); } else if (res >= 0 ) { System.out.println("VERY_WEAK"); }else { System.out.println( "Not a legal code grade: "+res); } } /* * 密码长度 * */ public static int checkLen(String str) { int score = 0; if (str.length() <= 4) { return 5; } if (str.length() <= 7 && str.length() >= 5) { return 10; } return 25; } /* * 字母 * */ public static int checkAlpha(String str) { int score = 0; int lowAlpha = 0; int capAlpha = 0; for (int i = 0; i < str.length(); i++) { if (Character.isLowerCase(str.charAt(i))) { lowAlpha++; } if (Character.isUpperCase(str.charAt(i))) { capAlpha++; } } if (lowAlpha == 0 || capAlpha == 0) { score = 0; } if ((lowAlpha == 0 && capAlpha != 0) || (lowAlpha != 0 && capAlpha == 0)) { score = 10; } if (lowAlpha != 0 && capAlpha != 0) { score = 20; } return score; /* * 0 0 无字母 * 1 0 || 0 1 全是大或全是小 * * */ } /* * 数字 * */ public static int checkNumber(String str) { int score = 0; int cnt = 0; for (int i = 0; i < str.length(); i++) { if (Character.isDigit(str.charAt(i))) { cnt++; } } if (cnt == 0) { score = 0; } if (cnt == 1) { score = 10; } if (cnt > 1) { score = 20; } return score; } /* * 符号 * */ public static int checkSymbol(String str) { int score = 0; int cnt = 0; // char[]={'!','"','#','$','%','&','\\','(',')','*','+',',','-','.','/', // } String charset = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; for (int i = 0; i < str.length(); i++) { String t = String.valueOf(str.charAt(i)); if (charset.contains(t)) { cnt++; } } if (cnt == 0) { score = 0; } if (cnt == 1) { score = 10; } if (cnt > 1) { score = 25; } return score; } public static int bonus_grade(String code){ if( checkAlpha(code)==10 && checkNumber(code)>=10 && checkSymbol(code)==0 ){ return 2; }else if( checkAlpha(code)==10 && checkNumber(code)>=10 && checkSymbol(code)>=10 ){ return 3; }else if( checkAlpha(code)==20 && checkNumber(code)>=10 && checkSymbol(code)>=10 ){ return 5; }else{ return 0; } } }