最近见到类似的内容,于是闲着无聊花了半个晚上写了判断多少年间有多少这样的天数。
思路是这样的:
先把当前的日期由年月日分开,然后统计年月日的位数,然后反过来再根据位数创建新的日期,并和原来的比较。最后天数加一不断循环。
结果是1年到9999年一共有4286天翻转相同。
21世纪一共有10天,也正好是国庆这几天。
最后上代码:
using System;
using static System.Console;
namespace MyClass
{
public class MyClass
{
public static void Main()
{
DateTime date = new DateTime(2001, 1, 1);
DateTime fDate = new DateTime(2099, 12, 31);
while (DateTime.Compare(date, fDate) < 0)
{
string reDateString = ReverseString(date.ToString("yyyyMd"));
if (reDateString == ReverseString(reDateString).Trim('0'))
{
WriteLine(date.ToString("D"));
}
date = date.AddDays(1);
}
}
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
}