京东 9.17笔试题 Java

笔试卷

选择题30题 + 编程2题

选择题涉及Shell+Java虚拟机+并发+数据结构等,不细讲

编程题

第一题 提取年份 AC

题目描述:

小明想从一段英文短文中提取潜在的年份信息,待匹配的年份的范围为1000年至3999年,包含1000和3999。

输入一段英文短文,按出现次序输出所提取到的所有可能的年份字符串。

输入描述: 单组输入,输入一段英文短文,可能包含字母大小写,标点符号及空格。(不超过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);
                }
            }
        }
    }
}

第二题 王子与公主 AC

题目描述: 在一个n行m列的二维地图中,王子的位置为(x1,y1),公主的位置为(x2,y2)。

在地图中设有一些障碍物,王子只能朝上、下、左、右四个方向行走,且不允许走出地图也不允许穿越障碍物。

请编写一个程序判断王子是否可以顺利走到公主所在的位置。

输入描述
多组输入,第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;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u014377853/article/details/108653154