题解:T716930 2026年

· · 题解

:::align{center}

T716930 题解

𝐏𝐚𝐫𝐭〔 𝟎𝟏 〕—— 理解题目

给定一个整数 N ,然后求日期2026/01/01前 N 天是什么时候。

𝐏𝐚𝐫𝐭〔 𝟎𝟐 〕—— 解决题目

n = int(input())

target_date = datetime(2026, 1, 1) result_date = target_date - timedelta(days=n) print(result_date.strftime("%Y/%m/%d"))

:::

:::success[[**AC** 代码(C++)](https://www.luogu.com.cn/record/258084503)]{open}
```cpp
#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;
}

:::

𝐏𝐚𝐫𝐭〔 𝟎𝟰 〕—— 注释

$\textcolor{#3498dd}{^2}$:`tm`,即`time`类,`<ctime>`的核心类,[官网](https://cppreference.cn/w/cpp/chrono/c/tm)。