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

密码验证合格程序

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

不用正则表达式的做法,纯靠算法,通俗易懂

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()){
        String pwd = sc.nextLine();
        if(pwd.length() <= 8){
            System.out.println("NG");
            continue;
        }
            int countKind = 0;
        String newPwd = pwd.trim();
        if(hasNum(newPwd)){
            countKind++;
        }
        if(hasSmall(newPwd)){
            countKind++;
        }
        if (hasBig(newPwd)){
            countKind++;
        }
        if(hasOther(newPwd)){
            countKind++;
        }
        if(countKind < 3){
            System.out.println("NG");
            continue;
        }
        if(contain(newPwd)){
            System.out.println("NG");
            continue;
        }
        System.out.println("OK");
    }
}

public static boolean contain(String newPwd){
    char[] chars = newPwd.toCharArray();
    for (int i = 0; i < chars.length-2;i++){
        String sub = newPwd.substring(i,i+3);
         String subPwd = newPwd.substring(i+2);
        if(containDupl(sub)){
            continue;
        }
        if(containSubString(subPwd,sub)){
            return true;
        }
    }
    return false;
}

public static boolean hasBig(String pwd){
    char[] chars = pwd.toCharArray();
    for (char c : chars) {
        if(c>='A' && c <='Z'){
            return true;
        }
    }
    return false;
}

public static boolean hasSmall(String pwd){
    char[] chars = pwd.toCharArray();
    for (char c : chars) {
        if(c>='a' && c<='z'){
            return true;
        }
    }
    return false;
}

public static boolean hasNum(String pwd){
    char[] chars = pwd.toCharArray();
    for (char c : chars) {
        if(c>='0' && c <='9'){
            return true;
        }
    }
    return false;
}

public static boolean hasOther(String pwd){
       char[] chars = pwd.toCharArray();
    for (char c : chars) {
        if(!hasBig(c+"") && !hasSmall(c+"") &&!hasNum(c+"")){
            return true;
        }
    }
    return false;
}

public static boolean containSubString(String pwd,String sub){
    if(pwd.contains(sub)){
        return true;
    }
    return false;
}

public static boolean containDupl(String str){
    char[] chars = str.toCharArray();
    for (int i = 0; i < chars.length-1; i++) {
        if(chars[i]==chars[i+1]){
            return true;
        }
    }
    return false;
}

}

全部评论

相关推荐

07-22 11:07
门头沟学院 Java
点赞 评论 收藏
分享
来个厂收我吧:首先,市场侧求职我不是很懂。 但是,如果hr把这份简历给我,我会觉得求职人不适合做产品经理。 问题点: 1,简历的字体格式不统一,排版不尽如人意 2,重点不突出,建议参考star法则写个人经历 3,印尼官方货币名称为印度尼西亚卢比(IDR),且GMV690000印尼盾换算为305人民币,总成交额不高。 4,右上角的意向职位在发给其他公司时记得删除。 5,你所有的经历都是新媒体运营,但是你要投市场营销岗位,jd和简历不匹配,建议用AI+提示词,参照多个jd改一下经历内容。 修改建议: 1,统一字体(中文:思源黑体或微软雅黑,英文数字:time new romans),在word中通过表格进行排版(b站学) 2,校招个人经历权重:实习经历=创业经历(大创另算)>项目经历>实训经历>校园经历 3,请将项目经历时间顺序改为倒序,最新的放最上方。 4,求职方向不同,简历文字描述侧重点也需要不同。
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

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