题解 | #日期累加#
日期累加
https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d
#include <cstdio> #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} }; int isleapyear(int y){ return (y%4==0&&y%100!=0 )||(y%400==0); } int main() { int n; cin>>n; while (n-->0) { int year,month,day,dd; cin>>year>>month>>day>>dd; int row=isleapyear(year); dd+=day; //全变成每月的1号 while (dd>dayTab[row][month]) { dd-=dayTab[row][month]; month++; if(month>12){ month=1; year++; row=isleapyear(year); } } printf("%04d-%02d-%02d\n",year,month,dd); } } // 64 位输出请用 printf("%lld")