题解 | #日期累加#
日期累加
https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d
#include <iostream> using namespace std; int GetMonthDay(int year, int month) { int monthDay[13] = { 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 n, y, m, d, sum; scanf("%d\n", &n); while (n--) { // 注意 while 处理多个 case scanf(" %d %d %d %d", &y, &m, &d, &sum); sum = sum + d; while (sum > GetMonthDay(y, m)) { sum -= GetMonthDay(y, m); m++; if (m > 12) { y++; m = 1; } } d = sum; printf("%04d-%02d-%02d\n", y, m, d); } } // 64 位输出请用 printf("%lld")