您的位置:首页 > 编程语言 > C语言/C++

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

2017-03-02 11:04 169 查看
 

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函数十分便捷,耗时较少
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++STL