E02724: 生日相同
implemtation, http://cs101.openjudge.cn/pctbook/E02724/
在一个有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来源:计算概论化学学院期末考试
根据学生的生日(月份和日期)找到所有生日相同的学生。
python
n = int(input())
# 创建一个字典存储生日到学号的映射
birthday_to_ids = {}
# 读取每个学生的数据并填充字典
for _ in range(n):
student_id, month, day = input().split()
month = int(month)
day = int(day)
birthday = (month, day)
if birthday not in birthday_to_ids:
birthday_to_ids[birthday] = []
birthday_to_ids[birthday].append(student_id)
# 输出生日相同的学生信息
output_lines = []
for birthday in sorted(birthday_to_ids.keys()):
students_with_same_birthday = birthday_to_ids[birthday]
if len(students_with_same_birthday) > 1: # 只关注有多个学生同一天生日的情况
output_line = f"{birthday[0]} {birthday[1]} " + " ".join(students_with_same_birthday)
output_lines.append(output_line)
for line in output_lines:
print(line)