题解 | #打印日期#
打印日期
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++#