/ 面经 / 24浏览

阿里4.15笔试

1. 九键输出字母

解答
通过全部测试用例
思路:暴力模拟

import java.util.Scanner;

public class AMain {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        StringBuilder stringBuilder = new StringBuilder();
        String s = in.next();
        char[] chars = s.toCharArray();
        Integer cur = null;
        int count = 0;

        for (char c : chars) {
            int i = c - '0';
            if (cur == null) {
                if (i != 1) {
                    cur = i;
                    count = 1;
                }
            } else if (cur == i) {
                count++;
            } else {
                stringBuilder.append((char) getChar(cur, count));
                if (i == 1) {
                    count = 0;
                    cur = null;
                } else {
                    cur = i;
                    count = 1;
                }
            }
        }

//        if (cur != null && count > 0) {
//            stringBuilder.append((char) getChar(cur, count));
//        }

        System.out.println(stringBuilder.toString());
    }

    public static int getChar(int num, int count) {

        if (num == 9 || num == 7) {
            count = (count - 1) % 4;
        } else {
            count = (count - 1) % 3;
        }

        if (num == 2) {
            return 'A' + count;
        } else if (num == 3) {
            return 'D' + count;
        } else if (num == 4) {
            return 'G' + count;
        } else if (num == 5) {
            return 'J' + count;
        } else if (num == 6) {
            return 'M' + count;
        } else if (num == 7) {
            return 'P' + count;
        } else if (num == 8) {
            return 'T' + count;
        } else if (num == 9) {
            return 'W' + count;
        }
        return 0;
    }
}

2. 四子棋

题目描述

image-1650080208048

输入描述

image-1650080264627

输出描述

image-1650080287864

示例

image-1650080312489

解答
思路:动态规划
image-1650080340735

import java.util.Scanner;

public class AMain2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] r1 = new int[n + 2][m + 2];//横
        int[][] r2 = new int[n + 2][m + 2];//竖
        int[][] r3 = new int[n + 2][m + 2];//右下
        int[][] r4 = new int[n + 2][m + 2];//左下
        int[][] p1 = new int[n + 2][m + 2];
        int[][] p2 = new int[n + 2][m + 2];
        int[][] p3 = new int[n + 2][m + 2];
        int[][] p4 = new int[n + 2][m + 2];

        for (int i = 0; i < n; i++) {
            char[] chars = in.next().toCharArray();

            for (int j = 0; j < chars.length; j++) {
                int r = i + 1;
                int c = j + 1;
                if (chars[j] == 'r') {
                    r1[r][c] = r1[r][c - 1] + 1;
                    r2[r][c] = r2[r - 1][c] + 1;
                    r3[r][c] = r3[r - 1][c - 1] + 1;
                    r4[r][c] = r4[r - 1][c + 1] + 1;
                } else if (chars[j] == 'p') {
                    p1[r][c] = p1[r][c - 1] + 1;
                    p2[r][c] = p2[r - 1][c] + 1;
                    p3[r][c] = p3[r - 1][c - 1] + 1;
                    p4[r][c] = p4[r - 1][c + 1] + 1;
                }
            }

        }

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (r1[i][j] >= 4 ||
                        r2[i][j] >= 4 ||
                        r3[i][j] >= 4 ||
                        r4[i][j] >= 4) {
                    System.out.println("kou");
                    return;
                } else if (p1[i][j] >= 4 ||
                        p2[i][j] >= 4 ||
                        p3[i][j] >= 4 ||
                        p4[i][j] >= 4) {
                    System.out.println("yukari");
                    return;
                }
            }
        }
        System.out.println("to be continued");
    }

}

3. 树的两个节点价值差最小值

题目描述

image-1650080454631

输入描述

image-1650080463500

输出描述

image-1650080473888

示例1

image-1650080501200
image-1650080519386

示例2

image-1650080540371

解答
思路:构造树、循环暴力
image-1650080563947