KY257 日期累加

KY257 日期累加

⭐️难度:中等

⭐️类型:模拟

📖题目:

🌟思路:

利用NextDay函数,一天一天地加。

📚题解:

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>

using namespace std;

void NextDay(int& year, int& month, int& day) {
    int dayOfMonth[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    int isLeap;  // 是否是闰年
    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {  // 闰年二月29天
        dayOfMonth[2] = 29;
    }
    day++;
    if (day > dayOfMonth[month]) {
        day = 1;
        month++;
    }
    if (month > 12) {
        month = 1;
        year++;
    }
}

int main() {
    int n;
    scanf("%d", &n);
    int year, month, day, sum;
    for (int i = 0;i < n;i++) {
        int count = 0;
        scanf("%d%d%d%d", &year, &month, &day, &sum);
        while (count < sum) {
            NextDay(year, month, day);
            count++;
        }
        printf("%04d-%02d-%02d\n", year, month, day);

    }

    return 0;
}
相关推荐
Tisfy5 天前
LeetCode 3870.统计范围内的逗号:模拟 或 一步计算
数学·算法·leetcode·题解·模拟·遍历
Tisfy12 天前
LeetCode 2058.找出临界点之间的最小和最大距离:遍历+遇到极值则更新(这种题谁空间复杂度不是O(1)啊)
linux·数据库·leetcode·链表·题解·模拟·遍历
吞下星星的少年·-·15 天前
The 2025 ICPC Asia East Continent Online Contest (I) A题(模拟+贪心)
算法·贪心·模拟
謓泽19 天前
『51单片机』AD/DA
单片机·嵌入式硬件·51单片机·模拟·ad·da·謓泽
_Narcissus_23 天前
枚举和模拟算法笔记
c语言·数据结构·c++·笔记·算法·模拟·枚举
Tisfy1 个月前
LeetCode 3090.每个字符最多出现两次的最长子字符串:二重循环 / 滑动窗口
算法·leetcode·字符串·题解·模拟·双指针·滑动窗口
OuO-21 个月前
笔试强训 Day 44:最小差值、kotori 和素因子、dd 爱科学 1.0
二分查找·动态规划·贪心·模拟·记忆化搜索·递归·状态表示优化
gjjwh2 个月前
【2026年7月】 X-Plane12 v12.4.3-r1 和谐版完整版免费分享
模拟
Tisfy2 个月前
LeetCode 3867.数对的最大公约数之和:按题目说的做(gcd)
算法·leetcode·题解·模拟·最大公约数·gcd
Tisfy3 个月前
LeetCode 3838.带权单词映射:求和、取模、拼接(附python一行版)
python·算法·leetcode·字符串·题解·模拟·取模