自己写的。
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static boolean isComplex(String str){ //利用ASCII码判断是否满足复杂度 int upper=0,lower=0,num=0,other=0; for(int temp : str.toCharArray()){ if(temp>=65 && temp<=90){ upper=1; }else if(temp>=97 && temp<=122){ lower=1; }else if(temp>=48 && temp<=57){ num=1; }else{ other=1; } if((upper+lower+num+other)>=3){ return true; } } return false; } public static boolean isNoRepetition(String str){ //判断是否有重复子串 int n=str.length(); for(int i=3;i<n/2;i++){ //i表示子串的长度 for(int j=0;j<n-i;j++){ String str1=str.substring(j,j+i); for(int k=j+i;k<=n-i;k++){ String str2=str.substring(k,k+i); if(str1.equals(str2)){ return false; } } } } return true; } public static void judge(String str){ if(str.length()>=8 && isComplex(str) && isNoRepetition(str)){ System.out.println("OK"); }else{ System.out.println("NG"); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String str=in.nextLine(); judge(str); } } }