题目:日期问题
小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在1960年1月1日至2059年12月31日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。
比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。
给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?
输入
一个日期,格式是"AA/BB/CC"。 (0 <= A, B, C <= 9)
输入
输出若干个不相同的日期,每个日期一行,格式是"yyyy-MM-dd"。多个日期按从早到晚排列。
样例输入
02/03/04
样例输出
2002-03-04
2004-02-03
2004-03-02
资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
注意:
main函数需要返回0;
只使用ANSI C/ANSI C++ 标准;
不要调用依赖于编译环境或操作系统的特殊函数。
所有依赖的函数必须明确地在源文件中 #include <xxx>
不能通过工程设置而省略常用头文件。
提交程序时,注意选择所期望的语言类型和编译器类型。
分析:这个题目在大体思路上来说并不难,只是在实际处理的时候有些复杂,需要思路清晰地对每一种情况日期地合法性进行判断,所以程序写出来应该不短,另外,还需要对日期进行排序。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 10
int resuslt[3];
bool avalible = true;
bool isleap(int year)
{
return (year % 4 == 0 && year % 100 != 0) || year % 400;
}
void f(int year, int month, int day)//定义函数校验日期
{
if (year >= 0 && year <= 59) //校验年
{
year += 2000;
}
else if (year >= 60 && year <= 99)
{
year += 1900;
}
else
{
avalible = false;
return;
}
if (month > 12 || month < 1 || day < 1 || day > 31) //校验月和日
{
avalible = false;
return;
}
switch (month)
{
case 2:
if (isleap(year) && day > 28)
{
avalible = false;
return;
}
if (!isleap(year) && day > 29)
{
avalible = false;
return;
}
break;
case 4:
if (day > 30)
return;
break;
case 6:
if (day > 30)
{
avalible = false;
return;
}
break;
case 9:
if (day > 30)
{
avalible = false;
return;
}
break;
case 11:
if (day > 30)
{
avalible = false;
return;
}
break;
default:
break;
}
resuslt[0] = year;
resuslt[1] = month;
resuslt[2] = day;
}
void swap(int *num1, int *num2)
{
int n;
n = *num1;
*num1 = *num2;
*num2 = n;
}
int main()
{
int a, b, c;
int ans[3][3] = {0};//定义一个二位数组存放日期
scanf("%d/%d/%d", &a, &b, &c);
//printf("%d,%d,%d",a,b,c);
for (int i = 0; i < 3; i++)
{
switch (i)
{
case 0:
{
f(a, b, c);
if (avalible)
{
ans[i][0] = resuslt[0];
ans[i][1] = resuslt[1];
ans[i][2] = resuslt[2];
}
avalible = true;
break;
}
case 1:
{
f(c, a, b);
if (avalible)
{
ans[i][0] = resuslt[0];
ans[i][1] = resuslt[1];
ans[i][2] = resuslt[2];
}
avalible = true;
break;
}
case 2:
{
f(c, b, a);
if (avalible)
{
ans[i][0] = resuslt[0];
ans[i][1] = resuslt[1];
ans[i][2] = resuslt[2];
}
avalible = true;
}
default:
break;
}
}
for (int i = 0; i < 2; i++) //排序
{
for (int j = i + 1; j < 3; j++)
{
if (ans[i][0] > ans[j][0])
{
swap(&ans[i][0], &ans[j][0]);
swap(&ans[i][1], &ans[j][1]);
swap(&ans[i][2], &ans[j][2]);
}
else if (ans[i][0] == ans[j][0])
{
if (ans[i][1] > ans[j][1])
{
swap(&ans[i][1], &ans[j][1]);
swap(&ans[i][2], &ans[j][2]);
}
else if (ans[i][1] == ans[j][1])
{
if (ans[i][2] > ans[j][2])
{
swap(&ans[i][2], &ans[j][2]);
}
else if (ans[i][2] == ans[j][2])
{
ans[j][2] = 0; //去重
}
}
}
}
}
for (int i = 0; i < 3; i++)//按照要求输出日期
{
if (ans[i][2] != 0)
{
if (ans[i][1] < 9 && ans[i][2] < 9)
{
printf("%d-0%d-0%d\n", ans[i][0], ans[i][1], ans[i][2]);
}
else if (ans[i][1] < 9 && ans[i][2] > 9)
{
printf("%d-0%d-%d\n", ans[i][0], ans[i][1], ans[i][2]);
}
else if (ans[i][1] > 9 && ans[i][2] < 9)
{
printf("%d-%d-0%d\n", ans[i][0], ans[i][1], ans[i][2]);
}
else
{
printf("%d-%d-%d\n", ans[i][0], ans[i][1], ans[i][2]);
}
}
}
return 0;
}
测试结果:
测试结果
这里是使用的数组来存放日期,因此在处理排序和去重的时候呢就显得比较繁琐,后来我在补课看到结构体的时候才发现如果采用结构体来存储时间,在处理上会简化一些。于是尝试着写了一个基于结构体的日期排序的程序,如下:
#include <stdio.h>
struct Date
{
int year;
int month;
int day;
};
Date date[3];
void swap(Date *date1,Date *date2)
{
Date tmp;
tmp = *date1;
*date1 = *date2;
*date2 = tmp;
}
void dateSort(struct Date date[])
{
struct Date tmp;
for (int i = 0; i < 2; i++)
{
for (int j = i + 1; j < 3; j++)
{
if (date[i].year > date[j].year)
{
swap(&date[i], &date[j]);
}
else if (date[i].year == date[j].year)
{
if (date[i].month > date[j].day)
{
swap(&date[i], &date[j]);
}
else if (date[i].month == date[i].month)
{
if (date[i].day > date[i].day)
{
swap(&date[i], &date[j]);
}
}
}
}
}
}
int main()
{
for (int i = 0; i < 3; i++)
{
scanf("%d/%d/%d",&date[i].year,&date[i].month,&date[i].day);
}
dateSort(date);
for (int i = 0; i < 3; i++)
{
printf("%d-%d-%d\n",date[i].year,date[i].month,date[i].day);
}
}
网友评论