题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

有点费脑子,不过做出来了,公共元素那块没有想象中的那么复杂。

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNext()) {
            String s = in.nextLine();
            String flag = "T";
            if (s.length() >= 8) {
                int a = 0, b = 0, c = 0, d = 0;
                for (int i = 0; i < s.length(); i++) {
                    char ch = s.charAt(i);
                    if (ch >= 'A' && ch <= 'Z') {
                        a = 1;
                    } else if (ch >= 'a' && ch <= 'z') {
                        b = 1;
                    } else if ( ch >= '0' && ch <= '9') {
                        c = 1;
                    } else {
                        if (ch == ' ' || ch == '\n') {
                            flag = "F";
                            break;
                        }
                        d = 1;
                    }
                }
                if ("T".equals(flag) && a + b + c + d >= 3 && s.length()>=4) {
                    // 公共元素
                    String ss;
                    for (int i = 4; i < s.length(); i++) {
                        ss = s.charAt(i-2)+""+s.charAt(i-1)+""+s.charAt(i);
                        String newS = s.substring(0,i-3);
                        if(newS.contains(ss)){
                            flag = "F";
                            break;
                        }
                    }
                } else {
                    flag = "F";
                }
                // System.out.print(a + " " + b + " " + c + " " + d + " ");
            } else {
                flag = "F";
            }

            if ("T".equals(flag)) {
                System.out.println("OK");
            } else {
                System.out.println("NG");
            }
        }
    }
}

全部评论

相关推荐

04-09 09:47
门头沟学院 Java
Arbelite_:2-3k,这工资还不如去摇奶茶
点赞 评论 收藏
分享
03-25 19:00
东北大学 Java
程序员牛肉:太好了,是聊天记录。不得不信了。 当个乐子看就好,不要散播焦虑
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务