caseTest

1:HJ 20密码验证合格程序

 

提交记录

代码:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String input = "";
        while (in.hasNext()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            input = input + s;
        }

        System.out.print(checkPassword(input) == true ? "OK" : "NG");
        return;

    }

    public static boolean checkPassword(String password) {
        // 1非空校验 长度校验>8
        if (null == password || password.length() < 9) {
            return false;
        }
        // 2 大小数字 字符串4含3校验 
        // 3 子串重复校验
        return checkRepeat(password);
    }

    public static boolean checkRepeat(String password) {
        // 非空校验 
        if (null == password) {
            return false;
        }
        //3 置入map 用来校验3个字符以上的重复
        Map<String, String> passMap = new HashMap();
        String last = password.substring(2);
        for (int i = 2; i < password.length(); i++) {
            String key = last + String.valueOf(password.charAt(i));
            if (passMap.containsKey(key)) {
                return false;
            } else {
                passMap.put(key, key);
                last = password.substring(i - 1, i + 1);
            }
        }
        return true;
    }

}

2:HJ86 求最大连续bit数

 代码:

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.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            System.out.println(haveOneNum(a));
        }
    }
    public static int haveOneNum(int input) {
        // 转换为2进制
        String s = Integer.toString(input, 2);
        String temp = "1", key = temp;
        int num = 1;
        while(num < s.length()){
           if(s.contains(key)){
              key = key + temp;
              num++ ;
           } else{
            return num--;
           }    
        }
        return 0;
    }
}

 3:HJ43 迷宫问题 没头绪

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值