密码验证合格程序
2018-03-10 15:56
190 查看
题目描述:
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串
输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NGimport java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext())
{
String res = "OK";
String str = scanner.nextLine();
if (str.length() <= 8) res = "NG";
else
{
for (int i = 0; i < str.length() - 2; i++)
{
String subStr = str.substring(i, i + 3);
if (str.substring(i + 1).contains(subStr))
{
res = "NG";
break;
}
}
if (res.equals("OK"))
{
int upp = 0;
int low = 0;
int num = 0;
int oth = 0;
for (char ch : str.toCharArray())
{
if (ch >= 'A' && ch <= 'Z') upp = 1;
else if (ch >= 'a' && ch <= 'z') low = 1;
else if (ch >= '0' && ch <= '9') num = 1;
else oth = 1;
}
if (upp + low + num + oth < 3) res = "NG";
}
}
System.out.println(res);
}
}
}
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串
输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NGimport java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext())
{
String res = "OK";
String str = scanner.nextLine();
if (str.length() <= 8) res = "NG";
else
{
for (int i = 0; i < str.length() - 2; i++)
{
String subStr = str.substring(i, i + 3);
if (str.substring(i + 1).contains(subStr))
{
res = "NG";
break;
}
}
if (res.equals("OK"))
{
int upp = 0;
int low = 0;
int num = 0;
int oth = 0;
for (char ch : str.toCharArray())
{
if (ch >= 'A' && ch <= 'Z') upp = 1;
else if (ch >= 'a' && ch <= 'z') low = 1;
else if (ch >= '0' && ch <= '9') num = 1;
else oth = 1;
}
if (upp + low + num + oth < 3) res = "NG";
}
}
System.out.println(res);
}
}
}
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String str = scanner.nextLine(); if (checkLength(str) && checkKinds(str) && checkRepeats(str)) System.out.println("OK"); else System.out.println("NG"); } } public static boolean checkLength(String string) { if (string == null || string.length() <= 8) return false; return true; } public static boolean checkKinds(String string) { int digit = 0, low = 0, high = 0, others = 0; for (char ch: string.toCharArray()) { if (ch >= 'A' && ch <= 'Z') high = 1; else if (ch >= 'a' && ch <= 'z') low = 1; else if (ch >= '0' && ch <= '9') digit = 1; else others = 1; } int total = digit + low + high + others; return total >= 3 ? true : false; } public static boolean checkRepeats(String string) { for (int i = 0; i < string.length() - 2; i++) { String subStr = string.substring(i, i + 3); if (string.substring(i + 1).contains(subStr)) return false; } return true; } }
相关文章推荐