HNUST 1395日期排序 sort函数 快速排序


 

1395: 日期排序

时间限制: 1 Sec   内存限制: 128 MB
提交: 157   解决: 104
[ 提交][ 状态][ 讨论版]

题目描述

有一些日期,日期格式为“MM/DD/YYYY”。编程将其按日期大小排列。

输入

输出

样例输入

15/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005

样例输出

15/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
    int year;
    int month;
    int day;
}stu[1000];

bool cmp(node x,node y)
{
    if(x.year!=y.year)
        return x.year<y.year;
    else if(x.month!=y.month)
        return x.month<y.month;
    else
        return x.day<y.day;
}
//定义结构体排序规则,默认为从低到高
int main()
{   int i=0;
    //freopen("e://in.txt","r",stdin);
    while(scanf("%d/%d/%d",&stu[i].day,&stu[i].month,&stu[i].year)!=EOF)
    {
        i++;
    }
    sort(stu,stu+i,cmp);
    for(int j=0;j<i;j++)
        printf("%02d/%02d/%d\n",stu[j].day,stu[j].month,stu[j].year);
    return 0;
}
//sort函数十分便捷,耗时较少
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值