算法
直接开一个每个月对应的天数的数组,然后对天数 + 1,从月到年逐次判断是否越界,一旦越界就变回 1
(这题数据不够强, 大家可以试试把date[0] ++ 去掉, 然后用 1 1999 12 31 来测试一下, 结果错了但ac了)
时间复杂度 $O(T)$
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main()
{
int T;
cin >> T;
while (T -- )
{
int date[3];
cin >> date[0] >> date[1] >> date[2];
date[2] ++ ;
if (date[2] > days[date[1]]) date[1] ++ , date[2] = 1;
if (date[1] > 12) date[1] = 1, date[0] ++ ;
printf("%04d-%02d-%02d\n", date[0], date[1], date[2]);
}
return 0;
}