【排序:结构体】生日

题目描述

cjf 君想调查学校 OI 组每个同学的生日,并按照年龄从大到小的顺序排序。但 cjf 君最近作业很多,没有时间,所以请你帮她排序。

输入格式

输入共有 n + 1 n + 1 n+1 行,

1 1 1 行为 OI 组总人数 n n n

2 2 2 行至第 n + 1 n+1 n+1 行分别是每人的姓名 s s s、出生年 y y y、月 m m m、日 d d d

输出格式

输出共有 n n n 行,

n n n 个生日从大到小同学的姓名。(如果有两个同学生日相同,输入靠后的同学先输出)

样例 #1

样例输入 #1

3
Yangchu 1992 4 23
Qiujingya 1993 10 13
Luowen 1991 8 1

样例输出 #1

Luowen
Yangchu
Qiujingya

提示

数据保证, 1 < n < 100 1<n<100 1<n<100 1 ≤ ∣ s ∣ < 20 1\leq |s|<20 1s<20。保证年月日实际存在,且年份 ∈ [ 1960 , 2020 ] \in [1960,2020] [1960,2020]

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Person
//定义结构体
{
	 char name[21];
	 int year;
	 int month;
	 int day;
}Person;

int main()
{
	Person temp;//中间变量
	int num;
	Person birthday[101];//结构体数组
	scanf("%d",&num);
	for(int i=0;i<num;i++)
	{
		scanf("%s",birthday[i].name );
		scanf("%d",&birthday[i].year );
		scanf("%d",&birthday[i].month );
		scanf("%d",&birthday[i].day );
	}
	//选择排序
	if(num>1)
	{
		for(int i=0;i<num-1;i++)
		{
			for(int j=i+1;j<num;j++)
			{
				if(birthday[i].year >birthday[j].year ||(birthday[i].year ==birthday[j].year && birthday[i].month >birthday[j].month)||(birthday[i].year ==birthday[j].year&& birthday[i].month ==birthday[j].month&& birthday[i].day >=birthday[j].day))
				{
					temp=birthday[i];
					birthday[i]=birthday[j];
					birthday[j]=temp;
				}
			}
		}
	}
	for(int i=0;i<num;i++)
	{
		printf("%s\n",birthday[i].name);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值