筆試卷
選擇題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;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/82606.html
標籤:AI
