int is_leap(int year)
{
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 1;
return 0;
}
int get_days(int year, int month)
{
if(month == 2) return months[month] + is_leap(year);
else return months[month];
}
void next()
{
if( ++ minute == 60)
{
minute = 0;
if( ++ hour == 24)
{
hour = 0;
week = (week + 1) % 7;
if( ++ day > get_days(year, month))
{
day = 1;
if( ++ month > 12)
{
month = 1;
year ++ ;
}
}
}
}
}
- 核心思想:遍历每一个时间点,对每一个时间点判断所需完成的操作命令(可能为check,可能为输出)
- 考点1:通过以上三个函数简单模拟求解一些“输出某年某月第几天”的问题
- 考点2:通过添加星期来提高难度。
这种情况下通常需要在前面的基础上,在模拟月份中添加 week 变量表示星期;
通过 week = (week + days) % 7等操作来计算星期;
例题:https://www.acwing.com/problem/content/description/3217/ - 考点3:需要处理复杂日期的存储, 或是在某日期安排某个事务。
这种情况往往需要使用结构体存储 Task, Timer,在结构体中可以添加所需的函数来辅助模拟。
例题:https://www.acwing.com/problem/content/3257/