蓝桥杯 日期计算

问题描述

  已知2011年11月11日是星期五,问YYYY年MM月DD日是星期几?
  注意考虑闰年的情况。尤其是逢百年不闰,逢400年闰的情况。

输入格式

  输入只有一行
  YYYY MM DD

输出格式

  输出只有一行
  W

样例输入

  2011 11 11

样例输出

  5

数据规模和约定

  1599 <= YYYY <= 2999
  1 <= MM <= 12
  1 <= DD <= 31,
  且确保测试样例中YYYY年MM月DD日是一个合理日期 1 <= W <= 7,
  分别代表周一到周日

思路解析

  用蔡勒公式可以简单的求解日期推算星期问题

代码

#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
	int year,month,day,x;
	scanf("%d %d %d",&year,&month,&day);
	if(month==1||month==2)
	{
		month+=12;
		year--;
	}
	if(year>=1582&&month>=10&&day>=4)
		x = (day +1+ 2 * month + 3 * ( month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7 ;  
	else
		x = (day +1+ 2 * month + 3 * ( month + 1) / 5 + year + year / 4 +5) % 7 ;  
	printf("%d\n", x);
	return 0;
}

输出结果

在这里插入图片描述

发布了95 篇原创文章 · 获赞 28 · 访问量 2417

猜你喜欢

转载自blog.csdn.net/qq_42093469/article/details/103358436
今日推荐