Recalling the written test questions of Ali 2021 interns in the spring recruitment

first question

The main idea of ​​the topic : tell you a two-dimensional table, which represents the urban orthogonal transportation network, @ means the starting point, the character'.' means it can pass, and the character # means no pain. A criminal is running away in a car. This car will not turn as long as the road is in front. When the road is blocked or the edge of the city, it will turn. After a total of k turns, where is the criminal now?

Enter a description:

3 4 4 //respectively represent the two-dimensional table m*n, and the number of turns
@…
.#…
…# //The city road indicated by the above m line is not accessible, @ as a pathway to see
EAST
SOUTH
WEST
NORTH //the above k lines Indicates the direction of the turn

Output description:

1 3 //Indicates the position in the first row and third column

My code:

import java.util.Arrays;
import java.util.Scanner;

public class Solution1 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
    
    
            int[] nums = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::valueOf).toArray();
            int row = nums[0];
            int col = nums[1];
            int k = nums[2];
            char[][] chars = new char[row][col];
            // 记录初始位置
            int initRow = 0, initCol = 0;
            for (int i = 0; i < row; i++) {
    
    
                char[] charArray = sc.nextLine().toCharArray();
                for (int j = 0; j < charArray.length; j++) {
    
    
                    if (charArray[j] == '@') {
    
    
                        initRow = i;
                        initCol = j;
                    }
                    chars[i][j] = charArray[j];
                }
            }
            // 开始走位置
            for (int i = 0; i < k; i++) {
    
    
                String direct = sc.nextLine();
                if ("EAST".equalsIgnoreCase(direct)) {
    
    
                    while (initCol < col && chars[initRow][initCol] != '#') initCol++;
                    initCol--; // 0, 3
                } else if ("SOUTH".equalsIgnoreCase(direct)) {
    
    
                    while (initRow < row && chars[initRow][initCol] != '#') initRow++;
                    initRow--; // 1, 3
                } else if ("WEST".equalsIgnoreCase(direct)) {
    
    
                    while (initCol >= 0 && chars[initRow][initCol] != '#') initCol--;
                    initCol++; // 1, 2
                } else {
    
    
                    while (initRow >= 0 && chars[initRow][initCol] != '#') initRow--;
                    initRow++; // 0, 2
                }
            }
            System.out.println((initRow + 1) + " " + (initCol + 1));
        }
    }
}

Second question

The main idea of ​​the topic : Xiao Ming and his two friends play games. Now there are n items (n is a multiple of 3), each item has a corresponding value, and n items form a circle. Now the rules of the game are as follows:
1) Every time Xiao Ming takes an item, Friend 1 takes the first item in the counterclockwise direction, and Friend 2 takes the first item in the clockwise direction.
Now Xiao Ming wants to know if he can What is the maximum sum of the value of the items obtained?

Input and output are very simple, one line represents the value of n items

My code:
This question is not completely finished. Because the first question took 40 minutes, the character constant was written incorrectly, "WEST" was written as "WETS", and it took time to debug to find out where the problem was.

Guess you like

Origin blog.csdn.net/weixin_42541360/article/details/114628022