题解 | #打印日期#

打印日期

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

#include <array>
#include <iostream>
using namespace std;
// 获取当月的天数
int GetMonthDay(int year,int month)
{
    std::array<int, 13>monthday = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    if(month == 2 && ((year %4 == 0 && year % 100 !=0) || year % 400 ==0))
    {
        return 29;
    }
    return monthday[month];     
}
int main()
{
    int year = 0;
    int yeardays = 0;

    while(cin>>year>>yeardays)
    {
        int curmonth;
        int curday;
        // 月份从一月开始往后走
        int month = 1;
        while(yeardays > GetMonthDay(year,month))
        {
            yeardays = yeardays - GetMonthDay(year, month);
            month++;
        }
        if(month <10 && yeardays < 10)
        {
            cout<<year<<'-'<<0<<month<<"-"<<0<<yeardays<<endl;
        }
        else if(month <10 && yeardays >= 10)
        {
            cout<<year<<'-'<<0<<month<<"-"<<yeardays<<endl;
        }
        else if(month >=10 && yeardays < 10)
        {
            cout<<year<<'-'<<month<<"-"<<0<<yeardays<<endl;
        }
        else 
        {
            cout<<year<<'-'<<month<<"-"<<yeardays<<endl;
        }
    }

}

#c++#
全部评论

相关推荐

10-17 10:05
已编辑
北华大学 全栈开发
牛客872465272号:掉头发了哥
点赞 评论 收藏
分享
11-18 09:44
Java
小白也想要offer:简历别放洋屁,搞不还还放错了,当然你投外企除外,以上纯属个人观点
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务