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;
}
相关推荐
吞下星星的少年·-·13 天前
Div.3 CF 1131A题(排序,模拟)
算法·模拟·排序
重生之小比特15 天前
【算法拾光】模拟|提莫攻击 + N 字形变换
算法·模拟
Tisfy1 个月前
LeetCode 2913.子数组不同元素数目的平方和 I:第一题先常数很大的O(n^2)
算法·leetcode·题解·模拟·暴力
Logic1011 个月前
C语言/数据结构字符串题解:解压缩“字符+次数“格式——多位数字处理与字符串还原
c语言·数据结构·字符串·模拟·时间复杂度·空间复杂度·算法题
jyyyx的算法博客1 个月前
机器人开箱子【贪心 + 模拟】
贪心·模拟
gjjwh2 个月前
【免费】9月和谐版XPlane12 12.4.3-r1免费下载
模拟
Logic1012 个月前
C语言/数据结构数组题解:数字列表加一(Plus One)——进位处理与数组扩展
c语言·数据结构·模拟·数组·时间复杂度·算法题·进位
Tisfy2 个月前
LeetCode 0836.矩形重叠:xy两方向分别看
数学·leetcode·题解·模拟
Tisfy2 个月前
LeetCode 3870.统计范围内的逗号:模拟 或 一步计算
数学·算法·leetcode·题解·模拟·遍历
Tisfy2 个月前
LeetCode 2058.找出临界点之间的最小和最大距离:遍历+遇到极值则更新(这种题谁空间复杂度不是O(1)啊)
linux·数据库·leetcode·链表·题解·模拟·遍历