题目描述
职工有职工号,姓名,年龄.输入n个职工的信息,找出3个年龄最小的职工打印出来。
输入描述:
输入第一行包括1个整数N,1<=N<=30,代表输入数据的个数。
接下来的N行有N个职工的信息:
包括职工号(整数), 姓名(字符串,长度不超过10), 年龄(1<=age<=100)。
输出描述:
可能有多组测试数据,对于每组数据,
输出结果行数为N和3的较小值,分别为年龄最小的职工的信息。
关键字顺序:年龄>工号>姓名,从小到大。 import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
Map<Integer,Integer> m1=new TreeMap<Integer,Integer>();
Map<Integer,String> m2=new TreeMap<Integer,String>();
while(scan.hasNext()){
String nn=scan.nextLine();
int N=Integer.parseInt(nn);
for(int i=0;i<N;i++){
String str=scan.nextLine();
String[] sa=str.split(" ");
m1.put(Integer.parseInt(sa[2]),Integer.parseInt(sa[0]));
m2.put(Integer.parseInt(sa[0]),sa[1]);
}
int num=0;
Iterator itr=m1.entrySet().iterator();
while(itr.hasNext() && num<3){
Map.Entry entry=(Map.Entry)itr.next();
System.out.println(entry.getValue()+" "+m2.get(entry.getValue())+" "+entry.getKey());
num++;
}
}
}
}测试用例:
5
37 28 3
77 72 98
93 100 95
39 66 13
64 44 37
对应输出应该为:
37 28 3
39 66 13
64 44 37
你的输出为:
75 39 1
以上测试用例在本机也没问题。