ZZULIOJ1211: 日期排序

1211: 日期排序

题目描述:
有一些日期,日期格式为“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
#include
#include
#define N 100000
#define SWAP(a,b,t){t = a; a = b; b = t;}

typedef struct DATE{
	int day;
	int month;
	int year;
}DT;

int sort(DT a1, DT a2)
{
	if(a1.year != a2.year){
		if(a1.year > a2.year)
			return 1;
	}
	else{
		if(a1.month != a2.month){
			if(a1.month > a2.month)
				return 1;
		}
		else{
			if(a1.day > a2.day)
				return 1;
		}
	}
	return 0;
}

int main()
{
	char c;
	DT a[N], temp;
	int i = 0, j, k;
	while(scanf("%d%c%d%c%d", &a[i].day, &c, &a[i].month, &c, &a[i].year) != EOF){
		i++;
	}
	for(j = 0; j < i-1; j++){
		for(k = j+1; k < i; k++){
			if(sort(a[j],a[k]))
				SWAP(a[j], a[k], temp);
		}
	}
	for(j = 0; j < i; j++)
		printf("%02d/%02d/%d\n",a[j].day, a[j].month, a[j].year);
	return 0;
}

思路:
1.利用结构体存储所有日期,再进行排序;
2.注意读入时可以用%c省略‘/’;

你可能感兴趣的:(zzulioj,c语言)