zoukankan      html  css  js  c++  java
  • 201503-3 节日

    蔡勒公式

    [{displaystyle w=left( left(y+left[{frac {y}{4}} ight]+left[{frac {c}{4}} ight]-2c+left[{frac {26(m+1)}{10}} ight]+d-1 ight){mod ; {7}} + 7 ight){mod ; {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
    ",year,month,day);
                        ++ans_cnt;
                    }
                }
            }
            if (ans_cnt == 0) {
                printf("none
    ");
            }
        }
    
    }
    
  • 相关阅读:
    python面试题
    面试总结
    552 Student Attendance Record II 学生出勤记录 II
    551 Student Attendance Record I 学生出勤纪录 I
    547 Friend Circles 朋友圈
    546 Remove Boxes 移除盒子
    543 Diameter of Binary Tree 二叉树的直径
    542 01 Matrix 01 矩阵
    3.1 特性
    2.6 datetime 模块
  • 原文地址:https://www.cnblogs.com/amonqsq/p/13572486.html
Copyright © 2011-2022 走看看