世界完全对称日
昨天2020年2月2日,很多人发20200202,然后看了一下这个叫世界完全对称日,变准备口算一下下次对称日是什么时候,觉得麻烦,便编写了一个程序计算。
和之前一篇blog日期问题相似,我们在这基础上,将输入的日期20200203这8个数组成的数字串,依次存储到数组中,即array[8] = {2,0,2,0,0,2,0,3}。这个的字符分片我用的是简单的除法和取余结合的操作。然后在对这个数组进行处理,从中间向两端进行比较。代码贴下,读者自取。
#include<iostream>
#include<cstdio>
using namespace std;
//能被400整除。或者能被4整除但不能被100整除。
#define isLeapYear(x) ((x%4 == 0)&&(x%100 != 0))||(x%400 == 0)? 1 : 0
//列举出一年十二个月中的每个月的天数
int monthDay[13][2] = {
0,0,
31,31,
28,29,
31,31,
30,30,
31,31,
30,30,
31,31,
31,31,
30,30,
31,31,
30,30,
31,31
};
class Date
{
public:
int year;
int month;
int day;
//自觉推算下一天
void getDay()
{
day++;
if(day > monthDay[month][isLeapYear(year)])
{
month ++;
day = 1;
}
if(month > 12)
{
year++;
month = 1;
}
}
};
//判断该日期是否为对称日期,日期格式为20200203,八个数组依次存储在数组中,或者从中间向两端比较
int judgeDay(int *array)
{
int i = 3;
int j = 4;
int cnt = 0;
while (i >= 0&&j < 8)
{
if(array[i] == array[j])
{
cnt++;
}
i--;
j++;
}
if(cnt == 4)
{
return 1;
}
else
{
return 0;
}
}
void symDay(int year,int month,int day)
{
Date date;
date.year = year;
date.month = month;
date.day = day;
int array[8];
int temp = 0;
do
{
//数字串拆分到数组中
array[7] = date.year/1000;
array[6] = date.year/100%10;
array[5] = date.year/10%10;
array[4] = date.year%10;
array[3] = date.month/10;
array[2] = date.month%10;
array[1] = date.day/10;
array[0] = date.day%10;
//判断该日期是否为对称日期
temp = judgeDay(array);
//转到下一天
date.getDay();
}while(temp == 0);
//输出得到的对称日期
cout<<"下一次的世界对称日:"<<endl;
for(int i = 7;i >= 0;i--)
{
cout<<array[i];
}
}
int main()
{
system("CLS");
cout<<"请输入当前日期(格式20200203):"<<endl;
int year,month,day;
scanf("%4d%2d%2d",&year,&month,&day);
symDay(year,month,day);
return 0;
}
测试结果与实际结果: