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 迷宫问题 没头绪