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
//sort函数十分便捷,耗时较少#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; }