【刷题】美国节日、蔡勒公式(一个推算周几的计算公式)

题目:
和中国的节日不同,美国的节假日通常是选择某个月的第几个星期几这种形式,因此每一年的放假日期都不相同。具体规则如下:

  • 1月1日:元旦
  • 1月的第三个星期一:马丁·路德·金纪念日
  • 2月的第三个星期一:总统节
  • 5月的最后一个星期一:阵亡将士纪念日
  • 7月4日:美国国庆
  • 9月的第一个星期一:劳动节
  • 11月的第四个星期四:感恩节
  • 12月25日:圣诞节
    现在给出一个年份,请你帮忙生成当年节日的日期。

蔡勒公式:

蔡勒(Zeller)公式,是一个计算星期的公式,随便给一个日期,就能用这个公式推算出是星期几。
在这里插入图片描述

#include <iostream>
#include <stdio.h>
using namespace std;

int Zeller(int year, int month, int day)
{
    if (month == 1 || month == 2)
    {
        month += 12;
        year--;
    }
    int y = year % 100;
    int c = year / 100;
    int w = ((c / 4) - 2 * c + y + (y / 4) + (13 * (month + 1) / 5) + day - 1) % 7;
    w = (w + 7) % 7;
    if (w == 0)
        w = 7;
    return w;
}

int day_of_demand(int year, int month, int count, int d_of_week)
{
    int week = Zeller(year, month, 1);
    int day = 1 + (count - 1) * 7 + (7 + d_of_week - week) % 7;
    return day;
}

void NewYear(int year)
{
    printf("%d-%02d-%02d\n", year, 1, 1);
}

void MartinLuther(int year)
{
    int day = day_of_demand(year, 1, 3, 1);
    printf("%d-%02d-%02d\n", year, 1, day);
}

void President(int year)
{
    int day = day_of_demand(year, 2, 3, 1);
    printf("%d-%02d-%02d\n", year, 2, day);
}

void Fallensoldiers(int year)
{
    int week = Zeller(year, 6, 1);
    int day = 31 - ((week == 1) ? 6 : (week - 2));
    printf("%d-%02d-%02d\n", year, 5, day);
}


void NationalDay(int year)
{
    printf("%d-%02d-%02d\n", year, 7, 4);
}

void Labour(int year)
{
    int day = day_of_demand(year, 9, 1, 1);
    printf("%d-%02d-%02d\n", year, 9, day);
}

void Thanks(int year)
{
    int day = day_of_demand(year, 11, 4, 4);
    printf("%d-%02d-%02d\n", year, 11, day);
}

void Christmas(int year)
{
    printf("%d-%02d-%02d\n", year, 12, 25);
}

int main()
{
    int year;
    while(cin >> year)
    {
        NewYear(year);
        MartinLuther(year);
        President(year);
        Fallensoldiers(year);
        NationalDay(year);
        Labour(year);
        Thanks(year);
        Christmas(year);
        cout << endl;
    }
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值