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;
}
相关推荐
List<String> error_P8 天前
蓝桥杯高频考点练习:模拟问题“球队比分类”
数据结构·python·算法·模拟·球队比分
七夜zippoe8 天前
模拟与存根实战:unittest.mock深度使用指南
linux·服务器·数据库·python·模拟·高级摸您
Tisfy13 天前
LeetCode 3379.转换数组:下标取模
算法·leetcode·题解·模拟·取模
Tisfy16 天前
LeetCode 3713.最长的平衡子串 I:计数(模拟)
算法·leetcode·题解·模拟
CUC-MenG17 天前
Codeforces Round 1078 (Div. 2) A,B,C,D,E,F1个人题解
数学·前缀和·dfs·模拟·dp·线性dp·后缀和·树上dp
Tisfy23 天前
LeetCode 3637.三段式数组 I:一次遍历(三种实现)
算法·leetcode·题解·模拟·数组·遍历·moines
-dzk-1 个月前
【代码随想录】LC 59.螺旋矩阵 II
c++·线性代数·算法·矩阵·模拟
码农幻想梦1 个月前
3476. WERTYU
模拟
码农幻想梦1 个月前
3381. 手机键盘
模拟
码农幻想梦1 个月前
KY110 日期差值
模拟