链接:https://ac.nowcoder.com/acm/contest/46295/Q
来源:牛客网
题目描述
给定公元2000年到公元3000年之间的某一天,请你给出该天的前天是哪一天。
(此处略去一张无用的图片)
输入描述
输入在一个日期,格式如"yyyy-mm-dd",题目保证所有输入日期为合法日期。
输出描述
在一行中输出日期,格式如"yyyy-mm-dd"。
示例1
输入
2020-11-15
输出
2020-11-13
备注:
注意日期格式,月份或者天数不足2位要补零。
分析 1
首先考虑输入输出,比较简单
//输入
scanf("%d-%d-%d");
//输出
//注意要补零
printf("%04d-%02d-%02d");
算某一天的前天是哪一天很简单,天数 - 2 就可以了。
然后考虑一些特殊情况,比如 9.1 的前一天是 8.30,前一天到上个月去了,月数要减一,天数也要跟着变化。
既然月数要减一,万一是一月怎么办?于是年数也可能要减一,比如 2022.1.1 的前一天就成了 2021.12.30。
天数怎么变呢?很简单,以 9.1 为例
天数为 1
1 - 2 = -1
-1 <= 0 所以月数 - 1,为八月
八月有 31 天,31 + (-1) = 30
所以前一天是 8.30
可见还得先求出每个月的天数。这就不得不要考虑二月了,于是涉及到了闰年的判断。
参考代码 1
#include <stdio.h>
//获取指定月的天数
int daysOfMonth(int year, int month)
{
switch (month)
{
//大月
case 1: case 3: case 5: case 7: case 8:
case 10: case 12:
return 31;
//二月 需要特殊处理
case 2:
//先判断是否为闰年
if (((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
return 29; //闰
else
return 28; //平
//剩下的都是小月
default:
return 30;
}
}
int main()
{
int year = 0, month = 0, day = 0;
scanf("%d-%d-%d", &year, &month, &day);
day -= 2;
if (day <= 0)
{
month--; //月份的退位
if (month <= 0)
{
year--; //年份的退位
month = 12;
}
day += daysOfMonth(year, month);
}
printf("%04d-%02d-%02d\n", year, month, day);
return 0;
}
我到底哪里错了?
请自查以下注意点:
- 月、年的退位
- 每月的 1、2 号,每年的 1.1、1.2
- 大小月的变化
- 每月的 1、2 号
- 闰平年的变化
- 判断是否为闰年的条件有两个
- 二月的天数很特殊
- 输出日期的格式
- 是否补 0
如果实在是找不出来错误在哪里的话,可以用下面的 Python 代码对你的程序进行测试
from datetime import datetime, timedelta
import os
import subprocess
print("开始测试数据")
date = datetime(2000, 1, 1)
# 从 2000.1.1 一直枚举到 3000.12.30
while date < datetime(3000, 12, 30):
# 获取前天日期
testStr = datetime.strftime(date, "%Y-%m-%d") # 转字符串
# 运行 C 语言程序
p = subprocess.Popen("c.exe", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# C 语言程序输出结果
cResult = p.communicate(input=(datetime.strftime(date, "%Y-%m-%d")+"\n").encode("ascii"))[0]
cResult = cResult.decode("ascii").replace("\r\n", "")
# python 输出结果
pyResult = datetime.strftime(date - timedelta(days=2), "%Y-%m-%d")
if not (cResult == pyResult):
print("错误:" + datetime.strftime(date, "%Y-%m-%d") + " 预期结果 " + pyResult + " 实际结果 " + cResult)
exit()
else:
print("正确:" + datetime.strftime(date, "%Y-%m-%d") + " 预期结果 " + pyResult + " 实际结果 " + cResult)
date += timedelta(days=1)
先编译你的 C 语言代码,把编译后的可执行程序重命名为 c.exe
,保存以上代码为 whats_the_fxxking_problem_with_my_code.py
,把两个文件放在同一文件夹下,然后在该文件夹下启动 Powershell/Cmd,运行 py whats_the_fxxking_problem_with_my_code.py
,等待测试完成即可。
* 分析 2
既然 Python 内置时间日期处理库,那么 C 语言有没有?要是有的话直接调不就好了吗?
没错,还真有,只不过没有 Python 的那么方便。
日期时间处理相关的函数都放在 time.h
头文件下。
下面简单做一下相关的笔记:
struct tm //时间日期结构体
time_t //时间戳(自 1970.1.1 00:00:00 以来的秒数)(实际上就是个整数)
//将 struct tm(时间日期结构体)转换为 time_t(时间戳)
time_t mktime( struct tm *arg );
//将指定 time_t(时间戳)转换为 UTC struct tm(时间日期结构体)
struct tm *gmtime ( const time_t *timer );
//格式化输出struct tm(时间日期结构体)
size_t strftime(char * str,
size_t count,
const char * format,
const struct tm *time
);
* 参考代码 2
#include <stdio.h>
#include <time.h>
int main()
{
struct tm date = { 0 }; //输入的日期
struct tm newDate = { 0 }; //前天的日期
time_t newTime = 0;
int year = 0, month = 0, day = 0;
//C 语言里有时间日期格式化输出,但是好像没有输入,
//所以输入还是得手动来
//输入日期
scanf("%d-%d-%d", &year, &month, &day);
//填充结构体数据
date.tm_year = year - 1900;
date.tm_mon = month - 1;
date.tm_mday = day;
//tm 结构体转化为 time_t 结构体(时间戳),
//再减去两天的秒数,求得前天的时间戳
newTime = mktime(&date) - (2 * 24 * 60 * 60);
//再将结果转换回 tm 结构体
newDate = *gmtime(&newTime);
//将 tm 结构体格式化输出
char result[64];
strftime(result, 64, "%Y-%m-%d", &newDate);
printf("%s\n", result);
return 0;
}