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

密码验证合格程序

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 str = in.next();
            if(str.length()<=8){
                System.out.println("NG");
                // 跳出当前循环
                continue;
            }
            int numCount = 0; // 是否有数字
            int upCount = 0; // 是否有大写
            int downCount = 0;// 是否有小写
            int otherCount = 0; // 是否有其他字符
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (c >= '0' && c <= '9') {
                    if (numCount == 0) {
                        numCount++;
                    }
                } else if (c >= 'A' && c <= 'Z') {
                    if (upCount == 0) {
                        upCount++;
                    }
                } else if (c >= 'a' && c <= 'z') {
                    if (downCount == 0) {
                        downCount++;
                    }
                }else{
                    if (otherCount == 0) {
                        otherCount++;
                    }
                }
            }
            if(numCount + upCount + downCount + otherCount < 3){
                System.out.println("NG");
                continue;
            }else{
                int error = 0;
                for(int i=0;i < str.length()-2; i++){
                    for(int j=i+1; j< str.length()-2; j++){
                        if(str.charAt(i) == str.charAt(j)){
                            if(str.charAt(i+1) == str.charAt(j+1)){
                                if(str.charAt(i+2) == str.charAt(j+2)){
                                    error = 1;
                                    break;
                                }
                            }
                        }
                    }
                    if(error == 1){
                        break;
                    }
                }
                if(error == 1){
                    System.out.println("NG");
                    continue;
                }
            }
            System.out.println("OK");
        }
    }
}

全部评论

相关推荐

09-01 16:09
门头沟学院 Java
点赞 评论 收藏
分享
09-24 18:30
已编辑
长春工业大学 产品经理
小肥罗:HR就是好人的缩写哈哈哈哈
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务