• 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;
    }

     

    作者:快雪
    本文版权归作者所有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    Centos7的iso everything与DVD以及Live的区别
    Spring的PropertyPlaceholderConfigurer应用
    阿里巴巴-德鲁伊druid连接池配置
    阿里巴巴-德鲁伊druid连接池配置
    旅游机票类专业名词---PNR
    旅游机票类专业名词---PNR
    ajax async异步
    ajax async异步
    Mybatis 示例之 SelectKey
    Mybatis 示例之 SelectKey
  • 原文地址:https://www.cnblogs.com/kuaixue/p/15632758.html
Copyright © 2020-2023  润新知