java题目 HJ20 密码验证合格程序

描述

密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有长度大于2的不含公共元素的子串重复 (注:其他符号不含空格或换行)
 
数据范围:输入的字符串长度满足 1 \le n \le 100 \1n100 

输入描述:

一组字符串。

输出描述:

如果符合要求输出:OK,否则输出NG

示例1

输入:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出:
OK
NG
NG
OK

 

 1 import java.io.*;
 2 import java.util.*;
 3 
 4 public class Main {
 5     public static void main(String[] args) throws IOException {
 6         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 7         String s;
 8         while((s = br.readLine()) != null) {
 9             if(s.length() <=8) {
10                 System.out.println("NG");
11                 continue;
12             }
13             //包括大小写字母.数字.其它符号,以上四种至少三种
14             int[] count = new int[4];
15             for(int i =0; i<s.length(); i++) {
16                 if(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
17                     count[0] = 1;
18                 } else if(s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
19                     count[1] = 1;
20                 } else if(s.charAt(i) >= '0' && s.charAt(i) <= '9') {
21                     count[2] = 1;
22                 } else {
23                     count[3] = 1;
24                 }
25             }
26             if((count[1]+count[2]+count[3]+count[0]) <3) {
27                 System.out.println("NG");
28                 continue;
29             }
30             
31             //不能有长度大于2的不含公共元素的子串重复 (注:其他符号不含空格或换行)
32             if(getString(s, 0, 3)) {
33                 System.out.println("NG");
34                 continue;
35             } else {
36                 System.out.println("OK");
37             }
38         }
39     }
40     
41     public static boolean getString(String s, int l, int r) {
42         if(r >= s.length()) {
43             return false;
44         }
45         if(s.substring(r).contains(s.substring(l, r))) {
46             return true;
47         } else {
48             return getString(s, l+1, r+1);
49         }
50     }
51 }

 

posted @ 2022-03-07 21:25  海漠  阅读(322)  评论(0编辑  收藏  举报