Jingdong 9.17 Written Exam Questions Java

Written test

30 multiple-choice questions + 2 programming questions

Multiple-choice questions involve Shell+Java virtual machine+concurrency+data structure, etc., not going into details

Programming questions

The first question is to extract the year AC

Title description:

Xiao Ming wants to extract potential year information from a short English text. The range of the year to be matched is from 1000 to 3999, including 1000 and 3999.

Enter a short English text, and output all possible year strings extracted in the order of appearance.

输入描述: 单组输入,输入一段英文短文,可能包含字母大小写,标点符号及空格。(不超过2000个字符)

输出描述: 输出所提取到的所有可能的年份字符串,两两之间用一个空格隔开。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    
    
    
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        List<Integer> res = new ArrayList<>();
        String input = in.nextLine();
        in.close();
        int count=0;
        for(int i=0;i<input.length();i++){
    
    
            if(input.charAt(i)>='0' && input.charAt(i)<='9'){
    
    
                if(count==0 && input.charAt(i)=='0'){
    
    
                    continue;
                }else{
    
    
                    count++;
                }
            }else{
    
    
                if(count==4){
    
    
                    res.add(Integer.parseInt(input.substring(i-count,i)));
                }
                count = 0;
            }
        }
        if(count==4){
    
    
            res.add(Integer.parseInt(input.substring(input.length()-count,input.length())));
        }
        boolean first= true;
        for(Integer s:res){
    
    
            if(s<=3999 && s>=1000){
    
    
                if(first){
    
    
                    System.out.print(s);
                    first=false;
                }else{
    
    
                    System.out.print(" "+s);
                }
            }
        }
    }
}

The second question prince and princess AC

Title description: In a two-dimensional map with n rows and m columns, the position of the prince is (x1, y1) and the position of the princess is (x2, y2).

There are some obstacles in the map. The prince can only walk in the four directions up, down, left, and right, and is not allowed to walk out of the map or cross obstacles.

Please write a program to judge whether the prince can go to the position of the princess smoothly.

输入描述
多组输入,第1行输入一个正整数T表示输入数据的组数。
对于每一组输入数据:输入n+1行。
其中,第1行输入两个正整数n和m表示地图的大小,n为行数,m为列数。(n<=100,m<=100)
接下来n行表示地图,每一行都有m个字符,其中S表示王子的位置,E表示公主的位置,'.'表示可以通行,
'#'表示障碍物(不能通行)。

输出描述
针对每一组输入数据,判断王子是否能够到达公主所在位置?如果可以输出“YES”,否则输出“NO”。
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    
    
    
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        Integer num = in.nextInt();
        for(int i=0;i<num;i++){
    
    
            int n = in.nextInt();
            int m = in.nextInt();
            Node start = null;
            Node end = null;
            int[][] maze = new int[n][m];
            for(int ni=0;ni<n;ni++){
    
    
                String str = in.next();
                for(int mi=0;mi<str.length();mi++){
    
    
                    if(str.charAt(mi)=='.'){
    
    
                        maze[ni][mi] = 0;
                    }else if(str.charAt(mi)=='E'){
    
    
                        maze[ni][mi] = 0;
                        end = new Node(ni, mi, null);
                    }else if(str.charAt(mi)=='S'){
    
    
                        maze[ni][mi] = 0;
                        start = new Node(ni, mi, null);
                    }else if(str.charAt(mi)=='#'){
    
    
                        maze[ni][mi] = 1;
                    }
                }
            }
            Queue<Node> queue=new LinkedList<Node>();
            queue.add(start);
            Boolean isFound = false;
            while (!queue.isEmpty()){
    
    
                Node node=queue.poll();
                Node temp;
                do {
    
    
                    temp=find(node, maze);
                    if (temp!=null){
    
    
                        queue.add(temp);
                    }
                }while (temp!=null);
                if (node.x==end.x &&node.y==end.y){
    
    
                    isFound = true;
                }
            }
            if(isFound){
    
    
                System.out.println("YES");
            }else{
    
    
                System.out.println("NO");
            }
        }
        in.close();
    }

    private static Node find(Node node, int[][] maze){
    
    
        if (node.x+1<maze.length && maze[node.x+1][node.y]==0){
    
    
            maze[node.x+1][node.y]=2;
            return new Node(node.x+1, node.y, node);
        }
        if (node.x-1>=0 && maze[node.x-1][node.y]==0){
    
    
            maze[node.x-1][node.y]=2;
            return new Node(node.x-1, node.y, node);
        }
        if (node.y+1<maze[0].length && maze[node.x][node.y+1]==0){
    
    
            maze[node.x][node.y+1]=2;
            return new Node(node.x, node.y+1, node);
        }
        if (node.y-1>=0 && maze[node.x][node.y-1]==0){
    
    
            maze[node.x][node.y-1]=2;
            return new Node(node.x, node.y-1, node);
        }
        return null;
    }

    static class Node{
    
    
        int x;
        int y;
        Node pre;

        public Node(int x, int y, Node pre) {
    
    
            this.x = x;
            this.y = y;
            this.pre = pre;
        }
    }
}

Guess you like

Origin blog.csdn.net/u014377853/article/details/108653154