Description
在一个有180人的大班级中,存在两个人生日相同的概率非常大,现给出每个学生的学号,出生月日。试找出所有生日相同的学生。
Input
第一行为整数n,表示有n个学生,n<100。
此后每行包含一个字符串和两个整数,分别表示学生的学号(字符串长度小于10)和出生月(1<=m<=12)日(1<=d<=31)。
学号、月、日之间用一个空格分隔。
Output
对每组生日相同的学生,输出一行,
其中前两个数字表示月和日,后面跟着所有在当天出生的学生的学号,数字、学号之间都用一个空格分隔。
对所有的输出,要求按日期从前到后的顺序输出。
对生日相同的学号,按输入的顺序输出。
Sample Input
6
00508192 3 2
00508153 4 5
00508172 3 2
00508023 4 5
00509122 4 5
00232323 7 7
Sample Output
3 2 00508192 00508172
4 5 00508153 00508023 00509122
Hint
没有生日相同的不输出!
解题思路:
一看到跟学生信息有关的题,第一个想到的就是用结构体,无非是一些跟学号,班级,姓名,成绩有关的题目。果然这题也不例外,就是把学生信息存储在结构体中,对结构体排个序,遍历一遍,输出生日相同的学生学号。
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct student
{
char id[15];
int month, date;
int xu; // 记录学生的输入顺序,因为题中提到对生日相同的学生,按输入顺序输出
};
bool cmp(student a,student b)
{
if(a.month == b.month )
{
if(a.date == b.date)
{
return a.xu < b.xu ;
}
else
{
return a.date < b.date ; // 按题目要求进行结构体排序
}
}
return a.month < b.month ;
}
int main()
{
student stu[105];
int n;
bool isBegin = true, isFirst = true;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%s %d %d", &stu[i].id, &stu[i].month, &stu[i].date);
stu[i].xu = i ;
}
sort(stu, stu+n, cmp);
for(int i = 0; i < n-1; i++)
{
if(stu[i].month == stu[i+1].month && stu[i].date == stu[i+1].date)
{
if(isBegin)
{
if(isFirst)
{
printf("%d %d %s", stu[i].month, stu[i].date, stu[i].id); // 先输出生日,第一组输出不需要换行
isFirst = false;
}
else
printf("\n%d %d %s",stu[i].month,stu[i].date,stu[i].id); // 之后每组输出得先换行,并且都先输出生日
isBegin = false;
}
printf(" %s",stu[i+1].id); // 之后跟的是输出学生学号
}
else
isBegin = true;
}
return 0;
}
---------------------
作者:userluoxuan
来源:CSDN
原文:https://blog.csdn.net/userluoxuan/article/details/37671995
版权声明:本文为博主原创文章,转载请附上博文链接!