编写程序,实现按出生日期排序(结构体专题)。

     输入n个人的信息(信息包含姓名、出生日期。其中出生日期又包含年、月、日三部分信息。),按生日的月份和日期升序输出所有人信息。输入要求:首先输入一个整数n(1<=n<=10),表示好友人数,然后输入n行,每行包含一个好友的信息:姓名(不超过8位),以及三个整数,分别表示出生日期的年月日。按过生日的先后(月份和日期)输出所有好友的姓名和出生日期,用空格隔开,出生日期的输出格式见输出样例。

输入样例:

3

Zhangling 1985 2 4

Wangliang 1985 12 11

Fangfang 1983 6 1

输出样例:

Zhangling 1985-02-04

Fangfang 1983-06-01

Wangliang 1985-12-11

代码思路:

1.考虑到选择结构体会很方便;

2.初始化结构体、基本变量。
3.做一个按需升序结构单元的函数"shengxu"

4.在"shengxu"前后分别做输入与输出。

#include<stdio.h>

struct score
{
	char name[100];
	int year;
	int month;
	int day;
}stu[10];

void shengxu(struct score stu[], int n);

int main()
{
	int n, i, j;

	scanf("%d", &n);

	for (i = 0; i < n; i++)
	{
		scanf("%s %d %d %d", stu[i].name, &stu[i].year, &stu[i].month, &stu[i].day);
	}
	shengxu(stu, n);

	for (i = 0; i < n; i++)
	{
		printf("%s %d-%02d-%02d\n", stu[i].name, stu[i].year, stu[i].month, stu[i].day);
	}
	return 0;
}
void shengxu(struct score stu[], int n)
{
	int i, j, temp = 0;

	for (i = 1; i < n; i++)
	{
		for (j = i + 1; j < n; j++)
		{
			if ((stu[i].month > stu[j].month) || (stu[i].month == stu[j].month && stu[i].month > stu[j].day))
			{
				temp = stu[j].month;
				stu[j].month = stu[i].month;
				stu[i].month = temp;
			}
		}
	}
}

仅供参考

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值