20、密码验证合格程序

题目描述

密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串

输入描述:

一组或多组长度超过2的子符串。每组占一行

输出描述:

如果符合要求输出:OK,否则输出NG
这里写图片描述


Java code:只通过了90%,10%没找见哪里出了毛病,尴尬。

import java.util.Scanner;

public class Main20 {

    static boolean len(String s){               //长度监测
        if (s.length()>8) {
            return true;
        } else {
            //System.out.println("length");
            return false;
        }
    }

    static boolean type(String s){              //组合检查
        char temp [] ;
        temp = s.toCharArray();
        int flag[] = {0,0,0,0};
        //boolean big = false,small = false,num = false,other = false;
        for (int i = 0; i < temp.length; i++) {
            if ((int)temp[i] >= 65 && (int)temp[i] <= 90) {
                //System.out.println("BIG");
                flag[0] = 1;
            }else if ((int)temp[i] >= 97 && (int)temp[i] <= 122) {
                //System.out.println("SMALL");
                //small = true;
                flag[1] = 1;
            }else if ((int)temp[i] >=48 &&(int)temp[i] <= 57) {
                //System.out.println("NUM");
                //num = true;
                flag[2] = 1;
            }else {
                //System.out.println("OTHER");
                //other = true;
                flag[3] = 1;
            }
        }
        //System.out.println(""+big + small+num+other);
        if ((flag[0]+flag[1]+flag[2]+flag[3])>=3) {
            return true;
        } else {

            return false;
        }
//      if ((big&&small&&num&&other) || (big||(small&&num&&other))
//              || (small||(big&&num&&other)) || (num||(big&&num&&other)) || (other||(big&&num&&small))) {
//          return true;
//      } else {
//          System.out.println("type");
//          return false;
//      }

    }

    static boolean sub(String s){               //子串检查
        int k = 0;  
        int max = 0;
        for (int i = 1; i < s.length(); i++) {  
            for (int j = 0; j < s.length() - i; j++) {  
                if (s.charAt(j) == s.charAt(i + j)) {  
                    k++;  
                } else {  
                    k = 0;  
                }
                if (k > max) {  
                    max = k;  

                } else{max =1;}
            }  
        }  
        if (max > 2) {  
            //System.out.println(max);
            //System.out.println("sub");
            return false;
        }else{
            //System.out.println(max);
            return true;
        }   
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextLine()) {

            String str = scan.nextLine();
            if (len(str)&&type(str)&&sub(str)) {

                System.out.println("OK");
            } else {
                //len(str);type(str);sub(str);
                System.out.println("NG");
            }
        }

    }

}

idea:
把每个条件限制写成一个函数,第三个是难点,我觉得那10%出在这个上面。第三个是求最长重复子串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值