201503-3 节日

蔡勒公式

\[{\displaystyle w=\left( \left(y+\left[{\frac {y}{4}}\right]+\left[{\frac {c}{4}}\right]-2c+\left[{\frac {26(m+1)}{10}}\right]+d-1\right){\bmod \; {7}} + 7\right){\bmod \; {7}}} \]

  • w :周期,0-星期日;1-星期一;2-星期二;3-星期三;4-星期四;5-星期五;6-星期六
  • c :(century)年份前两位数
  • y :(year)年份后两位数
  • m :月份,某年的1、2月要看作上一年的13、14月,yc也要进行相应的修改
  • d :日

实现

#include <cstdio>

int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

bool is_leep_year_and_mouth(int year,int month) {
    if ((year % 400 == 0 
    || (year % 100 != 0 && year % 4 == 0)) 
    && month == 2) {
        return true;
    } else {
        return false;
    }
}

int zeller_cong(int year, int month, int day) {
    if (month < 3) {
        month += 12;
        --year;
    }

    int c = int (year / 100);
    int y = year - 100 * c;
    
    int w = ((y + int(y / 4) + int(c / 4) - 2 * c + int(26 * (month + 1)) / 10 + day - 1) % 7 + 7) % 7;
    
    return w;
}

int main() {
    int month, count, weekday;
    int year1, year2;

    scanf("%d%d%d%d%d",&month,&count,&weekday,&year1,&year2);
    weekday = weekday % 7;

    for (int year = year1;year <= year2;++year) {
        int month_days = days[month - 1];
        int week_cnt = 0;
        int ans_cnt = 0;
        if (is_leep_year_and_mouth(year,month)) {
            month_days+=1;
        }
        for (int day = 1;day <= month_days;++day) {
            int week = zeller_cong(year, month, day);
            if (week == weekday) {
                ++week_cnt;
                if (week_cnt == count) {
                    printf("%04d/%02d/%02d\n",year,month,day);
                    ++ans_cnt;
                }
            }
        }
        if (ans_cnt == 0) {
            printf("none\n");
        }
    }

}
posted @ 2020-08-27 17:07  amonqsq  阅读(62)  评论(0编辑  收藏  举报