题解 | #打印日期#
打印日期
https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b
#include <iostream>
using namespace std;
class Date {
public:
Date(int y = 1, int m = 1, int d = 1) {
_year = y;
_month = m;
_day = d;
}
int Days(int y, int m) {
int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (m == 2 )
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
return 29;
return arr[m];
}
Date operator+(int n) {
Date tmp = *this;
tmp._day += n;
while (tmp._day > Days(tmp._year, tmp._month)) {
tmp._day -= Days(tmp._year, tmp._month);
tmp._month++;
while (tmp._month > 12) {
tmp._month -= 12;
tmp._year++;
}
}
return tmp;
}
Date& operator++() {
*this = *this + 1;
return *this;
}
int _year;
int _month;
int _day;
};
int main() {
int a, b;
while (cin >> a >> b) { // 注意 while 处理多个 case
Date d0(a);
while (--b > 0) {
++d0;
}
printf("%4d-%02d-%02d\n",d0._year,d0._month,d0._day);
}
}
// 64 位输出请用 printf("%lld")

查看12道真题和解析