Blue Bridge Cup ACM training questions: 【1162】Password

topic description

There is a saying circulating on the Internet: "I often float on the Internet, how can I not get stabbed~". In fact, it is not difficult to surf the Internet with peace of mind, just learn some security knowledge.
First, we need to set a secure password. So what kind of password is safe? Generally speaking, a relatively secure password should at least meet the following two conditions:
(1). The length of the password should be greater than or equal to 8, and should not exceed 16.
(2). The characters in the password should come from at least three of the four groups in the "Character Category" below.
The four character categories are:
1. Uppercase letters: A,B,C...Z;
2. Lowercase letters: a,b,c...z;
3. Numbers: 0,1,2... 9;
4. Special symbols: ~,!, @, #, $, %, ^;
Give you a password, your task is to judge whether it is a safe password.

enter

The first line of input data contains a number M, followed by M lines, each line has a password (the maximum length may be 50), and the password only includes the above four types of characters.

output

For each test instance, judge whether the password is a secure password, if yes, output YES, otherwise output NO.

sample input

3
a1b2c3d4
Linle@ACM
^~^@^@!%

sample output

NO
YES
NO

Code display:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] s = new String[n];
        for (int i=0;i<n;i++) {
            s[i] = sc.next();
        }
        //加入四个计数器
        int a=0,b=0,c=0,d=0;
        for (int i = 0; i < n; i++) {
            // 字符串转化为数组
            char[] ch = s[i].toCharArray();
            if (ch.length>=8 && ch.length<=16) {
                for (int j = 0; j < ch.length; j++) {
//如果该数组里只要有一个元素在“A”~“Z”之间,那么就满足该条件,后续无论出现几个元素满足,a恒等于1,下述同理可得。
                    if (ch[j]>='A' && ch[j]<='Z') {
                        a=1;
                    } else if(ch[j]>='a' && ch[j]<='z') {
                        b=1;
                    } else if (ch[j]>=0 && ch[j]<=9) {
                        c=1;
                    } else {
                        d=1;
                    }
                }
                if (a+b+c+d>=3) {
                    System.out.println("YES");
                } else {
                    System.out.println("NO");
                }
                //初始化计数器
                a=0;b=0;c=0;d=0;
            }
        }
    }
}

Assessment status: 

 

Guess you like

Origin blog.csdn.net/aDiaoYa_/article/details/123614933