回文日期

#include<iostream>
#include<string>
using namespace std;
int day1[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
int day2[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
/*
		解题思路:
		1.按回文规则得到新数组
		2.判断得到新数组日期是否合法
		2.1第一次需判断是否在原日期之后
		3.年份+1
		4.重复第1-3步
		5.输出正确答案
		回文日期第一行
		ABABBABA型日期
		1.按格式规则得到新数组
		2.判断是否合法
		3.前两位增加
		4.重复前三步
	*/
class Date
{
private:
	int d[8];
public:
	Date()
	{
		memset(d, 0, 8);
	}
	Date(string& s)
	{
		for (int i = 0; i < 8; i++)
		{
			d[i] = s[i] - '0';
		}
	}
	int Gety()
	{
		int year;
		return year = 1000 * d[0] + 100 * d[1] + 10 * d[2] + d[3];
	}
	int Getm()
	{
		int month;
		return month = 10 * d[4] + d[5];
	}
	int Getd()
	{
		int day;
		return day = 10 * d[6] + d[7];
	}
	//d[0]-d[3]->year
	//d[4]-d[5]->month
	//d[6]-d[7]->day
	bool IsLeapYear(int year)
	{
		if (year % 400 == 0 || year % 4 == 0 && year % 400 != 0)
			return true;
		else
			return false;
	}
	bool Judge()
	{
		if (IsLeapYear(Gety()))
		{
			if (Getd() <= day1[Getm()])
			{
				return true;
			}
			else
				return false;
		}
		else
		{
			if (Getd() <= day2[Getm()])
			{
				return true;
			}
			else
				return false;
		}
	}
	bool JudgePrior(int* d)
	{
		int mon = 10 * d[4] + d[5];
		int day = 10 * d[6] + d[7];
		if (Getm() > mon)
			return true;
		else if (Getm() == mon && Getd() > day)
			return true;
		else
			return false;
	}
	void Yearzj()
	{
		d[3]++;
		int i = 3;
		do
		{
			d[i - 1] += d[i] / 10;
			d[i] = d[i] % 10;
		} while (--i>0);
	}
	void Create()
	{
		for (int i = 4; i < 8; i++)
		{
			d[i] = d[7 - i];
		}
	}
	void Zj()
	{
		d[1]++;
		d[0] += d[1] / 10;
		d[1] %= 10;
	}
	//ABABBABA
	void Create2()
	{
		d[7] = d[5] = d[2] = d[0];
		d[6] = d[4] = d[3] = d[1];
	}
	void Next()
	{
		int original[8];
		bool first = true;
		for (int i = 0; i < 8; i++)
		{
			original[i] = d[i];
		}
		while (true)
		{
			int i = 0;
			this->Yearzj();
			this->Create();
			if (first)
			{
				first = false;
				if (this->JudgePrior(original) && this->Judge())
					break;
			}
			else if (this->Judge())
				break;
			else
				continue;
		}
		this->Print();

		//输出下一个回文日期
		for (int i = 0; i < 8; i++)
		{
			d[i] = original[i];
		}
		while (true)
		{
			this->Zj();
			this->Create2();
			if (this->Judge())
				break;
			else
				continue;
		}
		this->Print();
		//ABABBABA


	}
	void Print()
	{
		for (int i = 0; i < 8; i++)
			cout << d[i];
		cout << endl;
	}
};
int main()
{
	string s;
	cin >> s;
	Date D(s);
	D.Next();
	system("pause");
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值