题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); List<String> list = new ArrayList<>(); while (in.hasNext()) { String str = in.nextLine();//我没添加空输入break竟然也能通过题目?我这代码IDEA自己跑根本输出不了,有没有大佬告诉我为啥 if (str.length() < 8) { list.add("NG"); continue; } int[] count = new int[] {0, 0, 0, 0}; boolean pan = false; int[] max = new int[str.length() - 2]; for (int i = 0; i < str.length(); i++) { int f = (int) str.charAt(i); if (f > 47 && f < 58) count[0] = 1; else if (f > 64 && f < 91) count[1] = 1; else if (f > 96 && f < 123) count[2] = 1; else if (f == 32) pan = true; else count[3] = 1; if (i >= 2) max[i - 2] = f*f*f + (int) str.charAt(i - 1) + (int) str.charAt(i - 2); }//f的三次方是为了增加复杂度降低相同的可能性,毕竟密码长度有限,三次方就能通过了 if (pan) { list.add("NG"); continue; } boolean pan2 = false; for (int i = 0; i < max.length - 1; i++) { if (pan2) break; for (int j = i + 1; j < max.length; j++) { if (max[i] > max[j]) { int hh = max[i]; max[i] = max[j]; max[j] = hh; } else if (max[i] == max[j]) { pan2 = true; break; } } } if(pan2){ list.add("NG"); continue; } int sum = 0; for (int b : count) { sum = sum + b; } if (sum >= 3) list.add("OK"); else list.add("NG"); } for (String b : list) { System.out.println(b); } } }