标签:case 10 arr int days year 20211202
今天看到有人说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; }
标签:case,10,arr,int,days,year,20211202 来源: https://www.cnblogs.com/kuaixue/p/15632758.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。