输入的第一行为N,即学生的个数(N<=1000) 接下来的N行包括N个学生的信息,信息格式如下: 01 李江 男 21 02 刘唐 男 23 03 张军 男 19 04 王娜 女 19 然后输入一个M(M<=10000),接下来会有M行,代表M次查询,每行输入一个学号,格式如下: 02 03 01 04
输出M行,每行包括一个对应于查询的学生的信息。 如果没有对应的学生信息,则输出“No Answer!”
4 01 李江 男 21 02 刘唐 男 23 03 张军 男 19 04 王娜 女 19 5 02 03 01 04 03
02 刘唐 男 23 03 张军 男 19 01 李江 男 21 04 王娜 女 19 03 张军 男 19
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int size = Integer.parseInt(scanner.nextLine()); Map<String, String> map = new HashMap<>(); for (int i=0; i<size; i++) { String line = scanner.nextLine(); map.put(line.substring(0, line.indexOf(" ")), line); } size = Integer.parseInt(scanner.nextLine()); for (int i=0; i<size; i++) { System.out.println(map.getOrDefault(scanner.nextLine(), "No Answer!")); } } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Main { static class student { String id; String name; String sex; String age; @Override public String toString() { return id + " " + name + " " + sex + " " + age; } public student() { } public student(String id, String name, String sex, String age) { this.id = id; this.name = name; this.sex = sex; this.age = age; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); ArrayList<student> list = new ArrayList<>(); int n = Integer.parseInt(br.readLine()); for (int i = 0; i < n; i++) { String[] s = br.readLine().split(" "); student st = new student(s[0], s[1], s[2], s[3]); list.add(st); } int num = Integer.parseInt(br.readLine()); for (int i = 0; i < num; i++) { int flag = 0; String temps = br.readLine(); for (student ss : list) { if (ss.id.equals(temps)) { System.out.println(ss); flag = 1; break; } } if (flag == 0) System.out.println("No Answer!"); } } }
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); HashMap<Integer, Student> map = new HashMap<>(); for (int i = 0; i < n; i++) { int id = scanner.nextInt(); map.put(id,new Student(id,scanner.next(),scanner.next(),scanner.nextInt())); } int m = scanner.nextInt(); for (int i = 0; i < m; i++) { Student student = map.get(scanner.nextInt()); if (student!=null) System.out.println(student); else System.out.println("No Answer!"); } } public static class Student{ Integer id; String name; String sex; Integer age; public Student(Integer id, String name, String sex, Integer age) { this.id = id; this.name = name; this.sex = sex; this.age = age; } @Override public String toString() { return id+" "+name+" "+sex+" "+age; } } }