前言
应广大同学要求,开始以OD机考题作为练习题,看看算法和数据结构掌握情况。有需要练习的可以关注下。
描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
数据范围:输入的字符串长度满足 1≤𝑛≤100 1≤n≤100
输入描述:
一组字符串。
输出描述:
如果符合要求输出:OK,否则输出NG
示例1
输入:
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000输出:
OK NG NG OK
实现原理
1.每行的输入字符进行比较。
2.比较规则按题目要求即可。
实现代码
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.hasNext()){int num=0;int[] checks=new int[4];String str=in.next();if(str.length()<8){System.out.println("NG");continue;}boolean skip=false;for(int i=0;i<str.length();i++){if(str.charAt(i)-'0'>=0&&str.charAt(i)-'0'<=9){checks[0]=1;}else if(str.charAt(i)-'a'>=0&&str.charAt(i)-'a'<26){checks[1]=1;}else if(str.charAt(i)-'A'>=0&&str.charAt(i)-'A'<26){checks[2]=1;}else{checks[3]=1;}if(i<=str.length()-3){String tempStr=str.substring(i,i+3);String lastStr=str.substring(i+3,str.length());if(lastStr.contains(tempStr)){System.out.println("NG");skip=true;break;}}}if(skip){continue;}for(int j=0;j<4;j++){num+=checks[j];}if(num<3){System.out.println("NG");}else{System.out.println("OK");}}}}