题解:P13876 [蓝桥杯 2023 省 Java/Python A] 特殊日期
Swordfishqwq · · 题解
题目传送门
前言
这题是提交答案题,但是注意到时限给到了
思路
因为是提交答案题,我们就直接编写暴力枚举程序求出答案。
注意以下坑点:
- 因为含
2000000 年1 月1 日,而这是符合要求的,所以结果别忘了加1 。 - 注意判断闰年
2 月份是29 天的。 - 最外层循环范围应该是
\left[2000,2000000\right) 的。
我们可以先把每月天数存储在一个全局数组里,如果当前年份是闰年,就把数组里下标为
代码
#include<bits/stdc++.h>
using namespace std;
bool check(int y){ //判断是否为闰年
if((y%4==0 && y%100!=0) || y%400==0) return true;
else return false;
}
int cnt[18]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //每月天数
long long ans;
signed main(){
cin.tie(0)->sync_with_stdio(0);
for(int yy=2000;yy<2000000;yy++){ //确定年份
for(int mm=1;mm<=12;mm++){ //确定月份
if(check(yy)) cnt[2]=29; //闰年
for(int dd=1;dd<=cnt[mm];dd++){ //确定日期
if(yy%mm==0 && yy%dd==0) ans++; //符合要求
}
cnt[2]=28; //复原
}
}cout<<ans+1<<'\n'; //注意+1
return 0;
}
结果为
#include<bits/stdc++.h>
using namespace std;
signed main(){
cin.tie(0)->sync_with_stdio(0);
cout<<35813063<<'\n';
return 0;
}