【Java笔试强训】day10编程题

目录

  • 编程题
    • 井字棋
    • 密码强度等级

编程题

井字棋

【Java笔试强训】day10编程题_第1张图片

public class Main {
    public boolean checkWon(int[][] board) {
        // write code here
        // 1  0  1
        // 1 -1 -1
        // 1 -1  0
        int n = board.length;
        int sum = 0;
        int i = 0, j = 0;
        for (; i < n; i++) {
            for (; j < n; j++) {
                sum += board[i][j];
                if (sum == n) {
                    return true;
                }
            }
        }
        sum = 0;
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                sum += board[j][i];
                if (sum == n) {
                    return true;
                }
            }
        }
        sum = 0;
        for (i = 0; i < n; i++) {
                sum += board[i][i];
                if (sum == n) {
                    return true;
                }
        }
        sum = 0;
        for (i = 0; i < n; i++) {
                sum += board[i][n - 1 - i];
                if (sum == n) {
                    return true;
                }
        }
        return false;
    }
}

密码强度等级

【Java笔试强训】day10编程题_第2张图片

import java.util.Scanner;

@SuppressWarnings({"all"})
public class Main2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int len = s.length();
        int score = 0;
        score = getLength(s, len) + getNum(s, len) + getChar(s, len) + getString(s, len);
        level(score);
    }


    private static int getLength(String s, int len) {
        if (len >= 8) {
            return 25;
        } else if (len >= 5 && len <= 7) {
            return 10;
        } else if (len <= 4){
            return 4;
        }
        return 0;
    }

    private static int getNum(String s, int len) {
        int count = 0;
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) - '0' >= 0 && s.charAt(i) - '0' <= 9) {
                count++;
            }
        }
        if (count > 1) {
            return 20;
        } else if (count == 1) {
            return 10;
        } else {
            return 0;
        }
    }

    private static int getChar(String s, int len) {
        int count1 = 0;
        int count2 = 0;
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) >= 65 && s.charAt(i) <= 90) {
                count1++;
            } else if (s.charAt(i) >= 97 && s.charAt(i) <= 122) {
                count2++;
            }
        }
        if (count1 > 0 && count2 > 0) {
            return 20;
        } else if (count1 > 0 || count2 > 0) {
            return 10;
        } else {
            return 0;
        }
    }


    private static int getString(String s, int len) {
        int count = 0;
        for (int i = 0; i < len; i++) {
            if (!((s.charAt(i) >= 65 && s.charAt(i) <= 90))
                    && !(s.charAt(i) >= 97 && s.charAt(i) <= 122)
                    && !((s.charAt(i) - '0' >= 0 && s.charAt(i) - '0' <= 9))) {
                count++;
            }
        }
        if (count > 1) {
            return 25;
        } else if (count == 1) {
            return 10;
        } else {
            return 0;
        }
    }


    private static void level(int score) {
        if (score >= 90) {
            System.out.println("VERY_SECURE");
        } else if (score >= 80 && score < 90) {
            System.out.println("SECURE");
        } else if (score >= 70 && score < 80) {
            System.out.println("VERY_STRONG");
        } else if (score >= 60 && score < 70) {
            System.out.println("STRONG");
        } else if (score >= 50 && score < 60) {
            System.out.println("AVERAGE");
        } else if (score >= 25 && score < 50) {
            System.out.println("WEAK");
        } else if (score >= 0 && score < 25) {
            System.out.println("VERY_WEAK");
        }
    }
}

你可能感兴趣的:(【Java笔试强训】48day,java,开发语言,jvm)