日期排序

在这里插入代码片
Description
有一些日期,日期格式为“MM/DD/YYYY”。编程将其按日期大小排列。 
Input
Output
Sample Input
12/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005
Sample Output
12/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005

#include<stdio.h>
#include<stdlib.h>
struct riqi{
 int month,day,year;
}data[1000];
int comp(const void *p1,const void *p2)
{
 struct riqi *c = (struct riqi *)p1;
 struct riqi *d = (struct riqi *)p2;
 if(c->year != d->year)
 {
  return d->year < c->year?1:-1;
 }
 else if(c->month != d->month&&c->year == d->year)
 {
  return d->month < c->month?1:-1;
 }
 else
 {
  return d->day < c->day?1:-1;
 }
}
int main(void)
{
 int i = 0;
 while(scanf("%d/%d/%d",&data[i].month,&data[i].day,&data[i].year) != EOF)
 {
  i++;
 }
 qsort(data,i,sizeof(data[0]),comp);
 for(int n = 0;n < i;n++)
 {
  printf("%02d/%02d/%02d\n",data[n].month,data[n].day,data[n].year);
 }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值