题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
小蓝每天都锻炼身体。正常情况下,小蓝每天跑 1 千米。如果某天是周一或者月初(1 日),为了激励自己,小蓝要跑 2 千米。如果同时是周一或月初,小蓝也是跑 2 千米。小蓝跑步已经坚持了很长时间,从 2000 年 1 月 1 日周六(含)到 2020 年 10 月 1 日周四(含)。请问这段时间小蓝总共跑步多少千米?
运行限制
最大运行时间:1s
最大运行内存: 128M
解答
#include <iostream>
using namespace std;
bool leap(int year){//判断闰年
return (year%4==0 && year%100!=0)||(year%400==0);
}
int main()
{
int week=6;
int count=0;
int dayMax;
for(int year=2000;year<=2020;year++){
for(int month=1;month<=12;month++){
if(month==2&&leap(year)){//闰年
dayMax=29;
}
if(month==2&&!leap(year)){//非闰年
dayMax=28;
}
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
dayMax=31;
}
if(month==4||month==6||month==9||month==11){
dayMax=30;
}
for(int day=1;day<=dayMax;day++){
if(week==1||day==1){
count++;//多跑1公里
}
count++;
week++;
if(week>7){//星期超过7
week=1;
}
if(year==2020&&month==10&&day==1){
cout<<count<<endl;
}
}
}
}
}