题解 | #打印日期#

打印日期

https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b

#include <iostream>
using namespace std;

bool LeapYear(int year)
{
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    {
        return true;
    }
    return false;
}

int main() {
    int GetMonth[2][13] = 
    {
        {0,31,28,31,30,31,30,31,31,30,31,30,31},
        {0,31,29,31,30,31,30,31,31,30,31,30,31}
    };
    int year,month,day;
    int count;
    while(scanf("%d %d",&year,&count) != EOF)
    {
        int leapyear = LeapYear(year);
        month = 1;
        while(count > GetMonth[leapyear][month])
        {
            count -= GetMonth[leapyear][month];
            ++month;
        }
        day = count;
        printf("%04d-%02d-%02d\n",year,month,day);
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

吴offer选手:HR:我KPI到手了就行,合不合适关我什么事
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务