Hoj 2652 Thanks giving Day

题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2562

题意:求任意年的11月份的第四个星期日的具体日期(感恩节)的日期。

方法一:直接模拟,999年11月1日是星期五(可以试出来)。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    int a[10005];
    int t = 5;
    for(int i=1000; i<=9999; i++)
    {
        if(i%400 == 0 || (i%4 == 0 && i%100!=0))
        {
            t+=2;
        }
        else
        {
            t++;
        }
        t%=7;
        a[i] = t;
    }
    int n;
    while(scanf(" %d",&n)!=EOF)
    {
        int x=a[n]-4;
        printf("%d-11-",n);
        if(x<=0)
        {
            printf("%d\n",3*7-x+1);
        }
        else
        {
            printf("%d\n",4*7-x+1);
        }
    }
    return 0;
}
 

方法二:蔡勒公式

公式:

蔡勒公式: 

W =( [C/4] - 2C + y + [y/4] + [13×(M+1) / 5] + d - 1) % 7; 

W是星期几,C是世纪数减一,y是年份后两位,M是月份(从3月开始,1月和2月要按上一年的13月和 14月来算,这时C和y均按上一年取值),d是日数。

出W的值,再除以7,余几就是星期几,余数为0,则是星期天。

注意:[...]表示只取整数部分

注意:公式中如计算得出负数,不能按习惯的余数的概念求余数,只能按数论中的余数的定义求余。为了方便计算,我们可以给它加上一个7的整数倍,使

变为一个正数,比如加上7、14、21、28等,得到一个整数后, 再除以7,余几,说明这一天是星期几。

#include <cstdio>

int w,c,y,m,d;

int main()
{
  int x;
  m=11;
  while (scanf("%d",&y)==1)
  {
    d=1;
    printf("%d-11-",y);
    c=y/100;
    y%=100;
    w=(c>>2)-(c<<1)+y+(y>>2)+13*(m+1)/5+d-1;
    while (w<0) w+=7;
    w%=7;
    x=4-w;
    if (x<0) x+=7;
    d+=x+21;
    printf("%d\n",d);
  }
  return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值