题目:跑步锻炼 方法:模拟即可 题目链接:第14届蓝桥杯 C&C++ 组省赛夺奖班【第三期】 - 【学长带练】跑步锻炼 - 蓝桥云课 (lanqiao.cn)
题目思路:模拟日历从2000年至2020年,注意闰年的判断(能被4整除而不能被100整除或者能被400整除的为闰年),且闰年的二月为29天,非闰年的二月有28天,用day数组记录每年,固定月份的天数,用于模拟遍历。
#include <bits/stdc++.h> #define ios \ ios::sync_with_stdio(false); \ cin.tie(nullptr); \ cout.tie(nullptr) #define rep(i,a,n) for(int i=a;i<=n;i++) #define per(i,a,n) for(int i=n;i>=a;i--) #define lowbit(x) ((x) & - (x)) #define pb push_back #define SZ(v) ((int)v.size()) #define PI acos(-1) #define x first #define y second #define mem(a,b) memset((a),(b),sizeof(a)) using namespace std; typedef long long ll; const int INF=0x3f3f3f3f; const ll LLINF=0x3f3f3f3f3f3f3f3fLL; const double eps=1e-6; const int MAX=2e5+10; const ll mod=1e9+7; /********************************* std-head *********************************/ int day[]={0,31,29,31,30,31,30,31,31,30,31,30,31}; //模拟日期 int main(){ int yy=2000,week=6; int ans=0; for(;yy<=2020;yy++) { if(yy%4==0&&yy%100!=0 || yy%400==0) day[2]=29; //在第一个for循环中判断闰年,并更改条件 else day[2]=28; for(int mm=1;mm<=12;mm++) { for(int dd=1;dd<=day[mm];dd++) { if(dd==1 || week==1) ans+=2; else ans++; week++; if(week==8) week=1; if(yy==2020 && mm==10&& dd==1) break; //遍历到题目要求的终点,退出循环 } if(yy==2020 && mm==10) break; } if(yy==2020) break; } cout<<ans<<endl; return 0; }