【排序:结构体】生日

题目描述

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);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现该功能的C语言代码: ```c #include <stdio.h> #include <string.h> struct date { int year; int month; int day; }; struct contact { char name[20]; struct date birthday; char phone[12]; }; int main() { int n, i, j; struct contact list[20], temp; // 从键盘输入联系人信息 printf("请输入联系人个数:"); scanf("%d", &n); printf("请输入联系人的姓名、生日(年 月 日)、电话号码:\n"); for (i = 0; i < n; i++) { printf("联系人%d:", i + 1); scanf("%s %d %d %d %s", list[i].name, &list[i].birthday.year, &list[i].birthday.month, &list[i].birthday.day, list[i].phone); } // 按年龄从大到小排序 for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (list[j].birthday.year < list[j + 1].birthday.year || (list[j].birthday.year == list[j + 1].birthday.year && list[j].birthday.month < list[j + 1].birthday.month) || (list[j].birthday.year == list[j + 1].birthday.year && list[j].birthday.month == list[j + 1].birthday.month && list[j].birthday.day < list[j + 1].birthday.day)) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } // 输出排序后的联系人信息 printf("按年龄从大到小排序后的联系人信息为:\n"); for (i = 0; i < n; i++) { printf("联系人%d:姓名:%s 生日:%d年%d月%d日 电话号码:%s\n", i + 1, list[i].name, list[i].birthday.year, list[i].birthday.month, list[i].birthday.day, list[i].phone); } return 0; } ``` 程序中定义了两个结构体 `date` 和 `contact`,用来分别存储日期和联系人信息。首先从键盘输入联系人信息,然后按照年龄从大到小的顺序排序排序时,采用冒泡排序法,按照年、月、日的顺序比较两个联系人的生日。最后输出排序后的联系人信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值