编程语言
首页 > 编程语言> > Day10:算法强训(井字棋+密码强度等级)java+代码注释

Day10:算法强训(井字棋+密码强度等级)java+代码注释

作者:互联网

1.井字棋

井字棋_牛客题霸_牛客网

/**
     * 井字棋
     * 1、检查行方向=N true
     * 2.检查列方向=N
     * 3.检查主对角线=N
     * 4.检查副对角线=N
     */
    public boolean checkWon(int[][] board) {
        // write code here
        int N = board.length;
        int sum = 0;
        //检查每行相加是否为N
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                sum += board[i][j];
            }
            if(sum == N){
                return  true;
            }
        }
        //检查每列相加是否为N
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                sum += board[j][i];
            }
            if(sum == N){
                return  true;
            }
        }
        //检查主对角线相加是否为N
        for (int i = 0; i < N; i++) {
            sum += board[i][i];
            if(sum == N){
                return  true;
            }
        }
        //检查副对角线相加是否为N
        for (int i = 0; i < N; i++) {
            sum += board[i][N-i-1];
            if(sum == N){
                return  true;
            }
        }
        return false;


    }

2.密码强度等级

密码强度等级_牛客题霸_牛客网

/**
     * 密码强度等级
     * 1.写分数函数:a.密码长度 b、字母 c、 数字 d、符号
     * 2.判断奖励
     * 3.分数配对输出“”
     */
    public static void main(String[] args) {
        //从键盘获取多个密码串(字符串)
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String str = sc.nextLine();
            int sum1 = getLen(str);
            int sum2 = getChar(str);
            int sum3 = getNum(str);
            int sum4 = getSym(str);
            int sum = 0;
            if (sum2 == 20 && sum3 >= 1 && sum4 >= 1) {
                sum = sum1 + sum2 + sum3 + sum4 + 5;
            } else if (sum2 == 10 && sum3 >= 1 && sum4 >= 1) {
                sum = sum1 + sum2 + sum3 + sum4 + 3;
            } else if (sum2 == 10 && sum3 >= 1 && sum4 == 0) {
                sum = sum1 + sum2 + sum3 + sum4 + 2;
            } else {
                sum = sum1 + sum2 + sum3 + sum4;
            }
            if (sum >= 90) {
                System.out.println("VERY_SECURE");
            } else if (sum >= 80) {
                System.out.println("SECURE");
            } else if (sum >= 70) {
                System.out.println("VERY_STRONG");
            } else if (sum >= 60) {
                System.out.println("STRONG");
            } else if (sum >= 50) {
                System.out.println("AVERAGE");
            } else if (sum >= 25) {
                System.out.println("WEAK");
            } else if (sum >= 0) {
                System.out.println("VERY_WEAK");
            }

        }
    }
    //密码长度
    public static int getLen(String str){
        if (str.length()<= 4){
            return 5;
        }else if (7>= str.length() && str.length()>=5){
            return 10;
        }else if (str.length()>=8){
            return 25;
        }
        return 0;
    }
    //字母
    public  static  int getChar(String str){
        int small = 0;
        int big = 0;
        for (int i = 0; i < str.length(); i++) {
            //大写字母范围65-90
            if (str.charAt(i) >= 65 && str.charAt(i)<=90){
                big++;
            }else if(str.charAt(i)>=97 && str.charAt(i)<=122){
                small ++;
            }
        }
        if (small >0 && big >0){
            return 20;
        }else if (small>0 || big >0){
            return 10;
        }else{
            return 0;
        }
    }
    //数字
    public static  int getNum(String  str){
        int num = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i)-'0'>= 0 && str.charAt(i)-'0' <=9){
                num ++;
            }
        }
        if (num >1){
            return 20;
        }else if (num==1){
            return 10;
        }else {
            return 0;
        }
    }
    //符号
    public  static  int getSym(String str){
        int num = 0;
        for (int i = 0; i < str.length(); i++) {
            if(!(str.charAt(i) >= 65 && str.charAt(i)<=90)&&
            !(str.charAt(i)>=97 && str.charAt(i)<=122)&&
                    !(str.charAt(i)-'0'>= 0 && str.charAt(i)-'0' <=9)){
                num ++;
            }
        }
        if (num >1){
            return 25;
        }else if(num ==1){
            return 10;
        }else{
            return 0;
        }
    }

标签:强训,java,&&,int,sum,else,井字棋,str,return
来源: https://blog.csdn.net/weixin_46452998/article/details/123475039