题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.*;
import java.util.regex.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String result = "OK";
String content = in.nextLine();
if (content.length() <= 8) {
result = "NG";
System.out.println(result);continue;
}
char[] chs = content.toCharArray();
int isUppercase = 0;
int isLowcase = 0;
int isNumber = 0;
int isOtherChar = 0;
boolean hsaRepeated = false;
for (char c : chs) {
int v = Integer.valueOf(c);
// System.out.println(String.format("%s:%s",c,v));
if (v >= 97 & v <= 122) {
isLowcase = 1;
continue;
}
if (v >= 65 & v <= 90) {
isUppercase = 1;
continue;
}
if (v >= 48 && v <= 57) {
isNumber = 1;
continue;
}
isOtherChar = 1;
}
//System.out.println(String.format("%d-%d-%d-%d",isLowcase,isNumber,isOtherChar,isUppercase) );
if((isLowcase+isNumber+isOtherChar+isUppercase)<3){
result = "NG";
System.out.println(result);continue;
}
for(int i=0;i<chs.length;i++){
if(hasRepeated(i,content)){
hsaRepeated=true;
break;
}
}
if(hsaRepeated){
result = "NG";
System.out.println(result);continue;
}
printVal:System.out.println(result);
//boolean isMatch = Pattern.matches(pattern, content);
}
}
public static boolean hasRepeated(int curIndex,String val){
if(curIndex<2){
return false;
}
//System.out.println( "check:"+val.substring(curIndex-2,curIndex+1));
if(val.substring(curIndex).contains( val.substring(curIndex-2,curIndex+1))){
return true;
}
return false;
}
}
腾讯公司氛围 3634人发布