题解:P15963 日期统计

· · 题解

这道题是纯模拟。

从 2239 年 9 月 9 日开始,最外层循环枚举年份,第二层枚举月份,第三层枚举天数。

月份可直接枚举,天数可以开一个数组,把每个月的天数存进数组里会方便很多。

记得特判:闰年的 2 月 为 28 天。闰年的判断方法如题目所示。

不想特判最后一天,也就是 9876 年 1月 1 日怎么办——可以看出这一天不是可爱的,故直接不枚举就好啦。

模拟代码:

#include<bits/stdc++.h>
using namespace std;
int ans;
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool isLeap(int y){
    if(y%400==0) return true;
    if(y%100==0) return false;
    if(y%4==0) return true;
    return false;
}
bool check(int y,int m,int d){
    int cnt[10]={0};
    int t=y;
    while(t>0){
        cnt[t%10]++;
        t/=10;
    }
    t=m;
    while(t>0){
        cnt[t%10]++;
        t/=10;
    }
    t=d;
    while(t>0){
        cnt[t%10]++;
        t/=10;
    }
    int num=0,val=-1;
    for(int i=0;i<10;i++){
        if(cnt[i]>0){
            num++;
            if(val==-1) val=cnt[i];
            else if(cnt[i]!=val) return false;
        }
    }
    return num>0;
}
int main(){
    for(int y=2239;y<9876;y++){
        int sm=1,sd=1;
        if(y==2239) sm=9,sd=9;
        for(int m=sm;m<=12;m++){
            int ed=days[m];
            if(m==2&&isLeap(y)) ed=29;
            int startd=1;
            if(y==2239&&m==9) startd=9;
            for(int d=startd;d<=ed;d++) ans+=check(y,m,d);
        }
    }
    cout<<ans;
    return 0;
}

由于题目只要求输出答案(其实是一样的),所以输出 \text 210778 就好啦。