描述
在一个有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
解题思路:
看到学生后有一排信息,首先想到运用结构体,要求按各种顺序输出,然后要想到进行排序,最烦人的是要想到没有生日相同的数据不输出!!!(当时琢磨了半天哪里不对)
#include <iostream>
#include <cstring>
using namespace std;
struct birth
{
string id;
int month,day;
int time; //表示次序
};
birth num[100];
int main()
{
int n;
cin >>n;
for(int i=0;i<n;i++)
{
cin >>num[i].id>>num[i].month>>num[i].day;
num[i].time=i;
}
for (int i=0;i<n;i++) //进行排序
for(int j=i+1;j<n;j++)
{
if(num[i].month>num[j].month)
{
birth exc;
exc=num[i];
num[i]=num[j];
num[j]=exc;
}
if(num[i].month==num[j].month&&num[i].day>num[j].day)
{
birth exc;
exc=num[i];
num[i]=num[j];
num[j]=exc;
}
if(num[i].month==num[j].month&&num[i].day==num[j].day&&num[i].time>num[j].time)
{
birth exc;
exc=num[i];
num[i]=num[j];
num[j]=exc;
}
}
for(int i=0;i<n;i++)
{
while (num[i].month!=num[i+1].month||num[i].day!=num[i+1].day) //将没有生日相同的学生排除
{
++i;
if(i==n-1||i==n) //若该数据为数组的最后一个直接结束,不需要进行接下来的操作
goto end;
}
cout<<num[i].month<<" "<<num[i].day<<" "<<num[i].id;
while(num[i].month==num[i+1].month&&num[i].day==num[i+1].day) //输出生日相同的学生的学号
{
cout<<" "<<num[i+1].id;
i++;
}
cout<<endl;
}
end:
return 0;
}