题解 | #日期累加#
日期累加
https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d
#include <iostream> using namespace std; int daytab[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} }; bool isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } int main() { int m; cin >> m; while (m--) { int year, month, day, n; cin >> year >> month >> day >> n; int row = isLeapYear(year); while (n) { if (day + n <= daytab[row][month]) { day += n; break; } else { n -= daytab[row][month] - day; day = 0; if (month != 12)month++; else { year++; month = 1; row = isLeapYear(year); } } } printf("%04d-%02d-%02d\n", year, month, day); } } // 64 位输出请用 printf("%lld")