题解 | #日期累加#
日期累加
https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d
#include<cstdio> bool fun(int n){ return n % 400 == 0 || n % 100 != 0 && n % 4 == 0; } int main(){ int n; int mday[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; while (scanf("%d", &n) != EOF){ for (int i = 0; i < n; i++){ int year, month, day, gap; scanf("%d%d%d%d", &year, &month, &day, &gap); for (int i = 0; i < gap; i++){ if (day < mday[month]){ day++; } else if (fun(year) && month == 2 && day < mday[month]+1){ day++; } else if (!fun(year) && day == mday[month] && month != 12){ day = 1; month++; } else if (fun(year) && month == 2 && day == mday[month] + 1){ day = 1; month++; } else if (fun(year) && month != 2 && day == mday[month]&&month!=12){ day = 1; month++; } else { year++; month = 1; day = 1; } } printf("%04d-%02d-%02d\n", year, month, day); } } }