当前位置:网站首页>OpenJudge NOI 1.13 18:Tomorrow never knows?

OpenJudge NOI 1.13 18:Tomorrow never knows?

2022-06-11 02:49:00 Junyi_ noip

【 Topic link 】

OpenJudge NOI 1.13 18:Tomorrow never knows?

【 Topic test site 】

1. simulation

【 Their thinking 】

Simulate the change of date value
To calculate the next day of the current date , First the “ Japan ” increase 1,
If “ Japan ” The value of exceeds the number of days in the current month , Then the month increases 1,“ Japan ” Turn into 1.
If the value of the month increases 1 After that, it exceeded 12, Then the year increases 1, The month becomes 1.
Here we need to set up two functions
isLeap Function to determine whether a year is a leap year , aliquot 400 Or not divisible 100 But it can be divided 4 The year of is leap year .
getMonthDay Find the number of days in a certain month of a certain year , Here we will consider the impact of leap years .

【 Solution code 】

solution 1:

#include<bits/stdc++.h>
using namespace std;
bool isLeap(int y)
{
    
    return y%400 == 0 || y%100 != 0 && y%4 == 0;
}
int getMonthDay(int y, int m)
{
    
    if(m == 2)
    {
    
        if(isLeap(y))
            return 29;
        else
            return 28;
    }
    else if(m == 4 || m == 6 || m == 9 || m == 11)
        return 30;
    else
        return 31;
}
int main()
{
    
    int y, m, d;
    scanf("%d-%d-%d", &y, &m, &d);
    d++;
    if(d > getMonthDay(y, m))
    {
    
        d = 1;
        m++;
        if(m > 12)
        {
    
            y++;
            m = 1;
        }
    }
    printf("%d-%02d-%02d", y, m, d);
    return 0;
}
原网站

版权声明
本文为[Junyi_ noip]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110204013356.html