题目描述:
代码详解:
#include<stdio.h>
int main()
{
int months[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };//记录每个月份的日期
int run = 0;//用来记录跑步数
int k = 6;//用来记录当前天数是周几
for (int year = 2000; year <= 2020; year++)//循环年份
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)//闰年
{
months[2] = 29;
}
else
{
months[2] = 28;
}
for (int month = 1; month <= 12; month++)//循环月份
{
for (int day = 1; day <= months[month]; day++)//循环天数
{
run++;//正常情况:每天跑1km
if (k == 8)
{
k = 1;//天数超过7,回归1
}
if (k == 1 || day == 1)//如果为周一或者每月的一号,多跑1km
{
run++;
}
k++;//进入第二天
if (year == 2020 && month == 10 && day == 1)//到达最后一天,退出循环
{
printf("%d\n", run);
break;
}
}
}
}
return 0;
}