c语言生日相同结构体,生日相同(结构体排序)

#include

#include

#include

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值