题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String s = in.next();
char[] c = s.toCharArray();
int num = 0;
int small = 0;
int big = 0;
int other = 0;
boolean flag = false;
if(c.length > 8){
for(int i = 0; i < c.length; i++){
if('0' < c[i] && c[i] < '9'){
num = 1;
}
else if('a' < c[i] && c[i] < 'z'){
small = 1;
}
else if('A' < c[i] && c[i] < 'Z'){
big = 1;
}else{
other = 1;
}
}
if(num + small +big + other >= 3){
for(int j = 0; j + 3 < s.length(); j ++){
String s2 = s.substring(j, j+3);
String s3 = s.substring(0,j);
String s4 = s.substring(j+3, s.length());
if(s3.contains(s2) || s4.contains(s2)){
System.out.print("NG");
flag = false;
break;
}else{
flag = true;
}
}
}else{
System.out.print("NG");
}
}else{
System.out.print("NG");
}
if(flag){
System.out.print("OK");
}
}
}
}

查看3道真题和解析
