ccf练习题3. 相同生日

【问题描述】

在一个有n个人的大班级中,存在两个人生日相同的概率非常大,现给出每个学生的学号,出生月日,试找出所有生日相同的学生。

【输入形式】

第一行为整数n,表示有n个学生,n<=200。此后每行包含一个字符串和两个整数,分别表示学生的学号(字符串长度为11位)和出生月(1<=m<=12)日(1<=d<=31),学号、月、日之间用一个空格分隔。

【输出形式】

对每组生日相同的学生,输出一行,其中前两个数字表示月和日,后面跟着所有在当天出生的学生的学号,数字、学号之间都用一个空格分隔。对所有的输出,要求按日期从前到后的顺序输出。对生日相同的学号,按输入的顺序输出。

【样例输入】

6
07101020105 3 15
07101020115 4 5
07101020118 3 15
07101020108 4 5
07101020111 4 5
07101020121 8 10
【样例输出】

3 15 07101020105 07101020118
4 5 07101020115 07101020108 07101020111
8 10 07101020121

#include<iostream>
#include<algorithm>
using namespace std;
struct student{
	string xuehao;
	int month;
	int day;
	int order;
	student():month(0),day(0),order(0){
	}
};
bool compare(const student& a,const student& b){
	if(a.month!=b.month)
	return a.month<b.month;
	else if(a.day!=b.day)
	return a.day<b.day;
	else if(a.order!=b.order)
	return a.order<b.order;
	return 0;
}
int main(){
	int num;
	cin>>num;
	if(num==0)
	return 0;
	student stu[num];
	for(int i=0;i<num;i++){
		cin>>stu[i].xuehao>>stu[i].month>>stu[i].day;
		stu[i].order=i;
	} 
	sort(stu,stu+num,compare);
	cout<<stu[0].month<<" "<<stu[0].day<<" ";
	cout<<stu[0].xuehao;
	for(int i=1;i<num;i++){
		if(stu[i].month==stu[i-1].month&&stu[i].day==stu[i-1].day){
			cout<<" "<<stu[i].xuehao;
		}
		else if(stu[i].month!=stu[i-1].month||stu[i].day!=stu[i-1].day){
			cout<<endl<<stu[i].month<<" "<<stu[i].day<<" ";
			cout<<stu[i].xuehao;
		}
	}
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值