46:生日相同
生日相同
在一个有180人的大班级中,存在两个人生日相同的概率非常大,现给出每个学生的学号,出生月日。试找出所有生日相同的学生。
输入
第一行为整数n,表示有n个学生,n<100。此后每行包含一个字符串和两个整数,分别表示学生的学号(字符串长度小于10)和出生月(1<=m<=12)日(1<=d<=31)。学号、月、日之间用一个空格分隔。
输出
对每组生日相同的学生,输出一行,其中前两个数字表示月和日,后面跟着所有在当天出生的学生的学号,数字、学号之间都用一个空格分隔。对所有的输出,要求按日期从前到后的顺序输出。对生日相同的学号,按输入的顺序输出。
样例输入
5
00508192 3 2
00508153 4 5
00508172 3 2
00508023 4 5
00509122 4 5
样例输出
3 2 00508192 00508172
4 5 00508153 00508023 00509122
代码
import java.util.*;
import java.math.*;
public class Main
{
static String union(int month, int day)
{
String m = month+"";
String d = day+"";
if(month < 10)
m = "0" + m;
if(day < 10)
d = "0" + d;
return m + " " + d;
}
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
TreeMap<String,String> tm = new TreeMap<String, String>();
for(int i = 1; i <= n; i++)
{
String s = cin.next();
int month = cin.nextInt();
int day = cin.nextInt();
String key = union(month,day);
String value = tm.get(key);
if(value == null)
tm.put(key,s);
else
tm.put(key, value + " " + s);
}
Set<String> node = tm.keySet();
for(String flag:node)
{
String tmp = tm.get(flag);
if(tmp.indexOf(" ") != -1)
{
String[] ans = flag.split(" ");
System.out.println(Integer.parseInt(ans[0])+ " " + Integer.parseInt(ans[1]) + " " + tmp);
}
}
}
}