T716930 题解
𝐏𝐚𝐫𝐭〔 𝟎𝟏 〕—— 理解题目
给定一个整数 ,然后求日期2026/01/01前 天是什么时候。
𝐏𝐚𝐫𝐭〔 𝟎𝟐 〕—— 解决题目
- 计算日期:从
2026/01/01往前推 天,得到对应的日期。 - 格式化输出:确保月份和日期都是两位数字,不足时补
0(例如2025/03/31)。 - 使用 中的
datetime库可以快速地解决这个问题。 - 嗯?你用?
彳亍…… 众所周知,ctime里面有时间类std::tm,其格式为:C++ // 构造: std::tm t = {}; t.tm_year = 年 - 1900; t.tm_mon = 月 - 1; t.tm_mday = 日; t.tm_hour = 小时; // 在这道题里面,我们好像不需要小时,随便弄就行🤪 // 相减: std::time_t prev_time = std::mktime(&t) - n * 86400; // 输出: cout << std::put_time /* 打印时间 */ (std::localtime(&prev_time), "%Y/%m/%d" /* 格式 */) << "\n";
𝐏𝐚𝐫𝐭〔 𝟎𝟑 〕—— AC代码
AC 代码(Python)
from datetime import datetime, timedelta
# 导入"日期, 时间差"
n = int(input())
target_date = datetime(2026, 1, 1)
result_date = target_date - timedelta(days=n)
print(result_date.strftime("%Y/%m/%d"))AC 代码(C++)
#include <iostream>
#include <ctime>
#include <iomanip> // for put_time
using namespace std;
int n;
int main() {
cin >> n;
tm t = {};
t.tm_year = 2026 - 1900;
t.tm_mon = 1 - 1;
t.tm_mday = 1;
t.tm_hour = 12;
time_t prev_time = mktime(&t) - n * 86400;
cout << put_time(localtime(&prev_time), "%Y/%m/%d") << "\n";
return 0;
}