牛客网--密码验证合格程序(Java)
題目描述
密碼要求:
?
?
?
1.長度超過8位
?
?
?
2.包括大小寫字母.數字.其它符號,以上四種至少三種
?
?
?
3.不能有相同長度超2的子串重復
?
?
?
說明:長度超過2的子串
?
輸入描述:
一組或多組長度超過2的子符串。每組占一行
輸出描述:
如果符合要求輸出:OK,否則輸出NG
示例1
輸入
復制
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000輸出
復制
OK NG NG OK代碼:
import java.util.*;
public class Main{
? ? public static void main(String[] args)
? ? {
? ? ? ? Scanner sc = new Scanner(System.in);
? ? ? ? while(sc.hasNext())
? ? ? ? {
? ? ? ? ?? ?String x = sc.nextLine();
? ? ? ? ?? ?if(x.length()<=8)
? ? ? ? ?? ?{
? ? ? ? ?? ??? ?System.out.println("NG");
? ? ? ? ?? ?}else {
? ? ? ? ?? ??? ?int a=0,b=0,c=0,d=0;//a:大寫 ?b:小寫 ?c:數字 ?d:其他符號
? ? ? ? ?? ??? ?for(int i=0;i<x.length();i++) {
? ? ? ? ?? ??? ??? ?if(x.charAt(i)>='A'&&x.charAt(i)<='Z') {
? ? ? ? ?? ??? ??? ??? ?a=1;
? ? ? ? ?? ??? ??? ?}else if(x.charAt(i)>='a'&&x.charAt(i)<='z') {
? ? ? ? ?? ??? ??? ??? ?b=1;
? ? ? ? ?? ??? ??? ?}else if(x.charAt(i)>='0'&&x.charAt(i)<='9') {
? ? ? ? ?? ??? ??? ??? ?c=1;
? ? ? ? ?? ??? ??? ?}else {
? ? ? ? ?? ??? ??? ??? ?d=1;
? ? ? ? ?? ??? ??? ?}
? ? ? ? ?? ??? ??? ?if(a+b+c+d>=3) {
? ? ? ? ?? ??? ??? ??? ?break;
? ? ? ? ?? ??? ??? ?}
? ? ? ? ?? ??? ?}
? ? ? ? ?? ??? ?if(a+b+c+d<3) {
? ? ? ? ?? ??? ??? ?System.out.println("NG");
? ? ?? ??? ??? ?}else {
? ? ?? ??? ??? ??? ?boolean t = true;
? ? ?? ??? ??? ??? ?for(int i=0;i<x.length()-3;i++)
? ? ?? ??? ??? ??? ?{
? ? ?? ??? ??? ??? ??? ?String str1 = x.substring(i,i+3);
? ? ?? ??? ??? ??? ??? ?String str2 = x.substring(i+3,x.length());
? ? ?? ??? ??? ??? ??? ?if(str2.contains(str1)) {
? ? ?? ??? ??? ??? ??? ??? ?System.out.println("NG");
? ? ?? ??? ??? ??? ??? ??? ?t = false;
? ? ?? ??? ??? ??? ??? ??? ?break;
? ? ?? ??? ??? ??? ??? ?}
? ? ?? ??? ??? ??? ?}
? ? ?? ??? ??? ??? ?if(t) {
? ? ?? ??? ??? ??? ??? ?System.out.println("OK");
? ? ?? ??? ??? ??? ?}
? ? ?? ??? ??? ?}
? ? ? ? ?? ?}
? ? ? ? }
? ? }
}
總結
以上是生活随笔為你收集整理的牛客网--密码验证合格程序(Java)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【剑指offer】面试题65:不用加减乘
- 下一篇: 【剑指offer】面试题57 - II: