今天看到有人说20211202这一天很特殊,想起刚学编程那会求某一范围内的回文数。今天闲来无事,计算了一下本世纪的回文日期,发现只有十一个,差不多十年一遇,今天已经是第四个了。希望看到本文的人都能活到2099。^_^
#include <iostream> #include<iomanip> using namespace std; int GetDays(int year, int month) { int days = -1; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: { days = 31; } break; case 4: case 6: case 9: case 11: { days = 30; } break; case 2: { days = 28 + (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0; } break; default: days = -1; break; } return days; } bool IsPalindromeDay(int year, int month, int day) { int n1 = day % 10; int n2 = day / 10; int n3 = month % 10; int n4 = month / 10; int n5 = year % 10; int n6 = (year / 10) % 10; int n7 = (year / 100) % 10; int n8 = (year / 1000) % 10; int arr[] = {n8, n7, n6, n5, n4, n3, n2, n1}; int count = sizeof(arr) / sizeof(arr[0]); int i = 0; while ((arr[i] == 0) && (i < (count - 5))) { ++i; } int j = count - 1; while (j > i) { if (arr[i++] != arr[j--]) { return false; } } return true; } int main() { int c = 0; for (int year = 2000; year <= 2099; ++year) { for (int month = 1; month <= 12; ++month) { int days = GetDays(year, month); for (int day = 1; day <= days; ++day) { if (IsPalindromeDay(year, month, day)) { cout << setw(4) << setfill(' ') << ++c << " : " << year << "-" << setw(2) << setfill('0') << month << "-" << setw(2) << setfill('0') << day << endl; } } } } return 0; }