先是自己写的,用Scanner读取,超时(说是因为这个)
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main{
public Main(){
}
public Main(String candidateNumber, String tryNum, String seatNum){
this.candidateNumber = candidateNumber;
this.tryNum = tryNum;
this.seatNum = seatNum;
}
private String candidateNumber;
public String getCandidateNumber() {
return candidateNumber;
}
private String tryNum;
public String getTryNum() {
return tryNum;
}
private String seatNum;
public String getSeatNum() {
return seatNum;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int num = input.nextInt();
String candidateNumber, tryNum, seatNum;
Main[] ans = new Main[num];
for (int i = 0; i < num; i++){
ans[i] = new Main(input.next(),input.next(),input.next());
}
// System.out.println(ans[0].getSeatNum());
int num2 = input.nextInt();
String[] ans2 = new String[num2];
for (int k = 0; k < num2; k++){
ans2[k] = input.next();
}
// System.out.println(ans2[1]);
for (int i = 0; i < num2; i++){
for (int j = 0; j < num; j++){
if (ans[j].getTryNum().equals(ans2[i])){
System.out.println(ans[j].getCandidateNumber() + " " + ans[j].getSeatNum());
break;
}
}
}
}
}
然后是借鉴别人的代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 输入第一行给出一个正整数 N(≤1000)
//需要将字符串转换为整数
int n = Integer.parseInt(br.readLine());
HashMap<String,String> hm = new HashMap<String,String>();
//随后 N 行,每行给出一个考生的信息
for (int i = 0; i<n; i++) {
//根据给定正则表达式的匹配拆分此字符串,所得数组中不包括结尾空字符串。
String[] stu = br.readLine().split(" ");
String result = stu[0]+" "+stu[2];
//stu[0] : 准考证号 stu[1] 试机座位号 stu[2] 考试座位号
//将试机座位号作为关键字,装入HashMap中
hm.put(stu[1], result);
}
//给出一个正整数 M(≤N),
int m = Integer.parseInt(br.readLine());
//随后一行中给出 M 个待查询的试机座位号码,以空格分隔。
String[] find = br.readLine().split(" ");
for (int i = 0; i<m ; i++) {
System.out.println(hm.get(find[i]));
}
}
}