The Same Calendar

链接:http://acm.hust.edu.cn/vjudge/problem/401743/origin

题目:

The girl Taylor has a beautiful calendar for the year y. In the calendar all days are given with their days of week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.

The calendar is so beautiful that she wants to know what is the next year after y when the calendar will be exactly the same. Help Taylor to find that year.

Note that leap years has 366 days. The year is leap if it is divisible by 400 or it is divisible by 4, but not by 100 (https://en.wikipedia.org/wiki/Leap_year).



题意:找到下一个整个日历和今年一模一样的年份(也就是某月的某一天同样是星期几)。

分析:首先要判断是不是闰年,因为要保证下一个年份跟这个年份同闰,在之后保证星期几是相同的,只要这两个不同年份的同一月的同一天之间相差的天数是7的整数倍就好了。

题解:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cstring>
#include <functional>
#include <cmath>
using namespace std;

int y;
bool isren(int year)
{
	if(year%400==0||(year%4==0&&year%100!=0))
		return true;
	return false;
}
int main()
{
	//freopen("in.txt","r",stdin);
	while(~scanf("%d",&y))
	{
		int ans=0;
		bool renflag=false;
		if(isren(y))
		{
			ans+=2;//366%7
			renflag=true;
		}
		else
			ans+=1;//365
		for(int i=1;;i++)
		{
			if(isren(++y))
				ans+=2;
			else
				ans+=1;
			if(ans%7==0&&renflag&&isren(y+1))
				break;
			if(!renflag&&ans%7==0&&!isren(y+1))
				break;
		}
		y++;
		printf("%d\n",y);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值