程序员的小节日__回文日

回文日

今天是2018年10月2日。2018102,倒着读也是一样的。我们定义这样的节日为回文日。

求回文日



#include <cstdio>
#include<cstdlib>

using namespace std;

bool panduan(int x)
{
    int y=0;
    int temp=x;
    while(temp!=0)
    {
        y=y*10+temp%10;
        temp=temp/10;
    }
    if(x==y)
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool run(int year)
{
    if((year%4==0&&year%100!=0)||year%400==0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
int getwei(int x)
{
    int res=0;
    while(x!=0)
    {
        x=x/10;
        res++;
    }
    return res;
}
typedef struct node
{
    int year;
    int month;
    int day;
}node;

int tuntores(node a)
{
    int res=0;
    int temp=getwei(a.month);
    res+=a.year;
    for(int i=0;i<temp;i++)
    {
        res*=10;
    }
    res+=a.month;
    temp=getwei(a.day);
    for(int i=0;i<temp;i++)
    {
        res*=10;
    }
    res+=a.day;
    return res;
}
node nextday(node a)
{
    a.day++;
    if(a.month==1||a.month==3||a.month==5||a.month==7||a.month==8||a.month==10||a.month==12)
    {
        if(a.day>31)
        {
            a.month++;
            a.day=1;
        }
    }
    else if(a.month==2)
    {
        if(run(a.year))
        {
            if(a.day>29)
            {
              a.month=3;
              a.day=1;
            }
        }
        else
        {
            if(a.day>28)
            {
                a.month=3;
                a.day=1;
            }
        }
    }
    else
    {
        if(a.day>30)
        {
            a.month++;
            a.day=1;
        }
    }
    if(a.month>12)
    {
        a.month=1;
        a.year++;
    }
    return a;
}
int main()
{
    node today;
    today.year=2018;
    today.month=10;
    today.day=3;
    printf("最近100个回文日:\n");
    for(int i=1;i<101;i++)
    {
        while(panduan(tuntores(today))==0)
        {
            today=nextday(today);
        }
        printf("%4d年%2d月%2d日 | \t",today.year,today.month,today.day);
        today=nextday(today);
        if(i%5==0)
        {
            printf("\n");
        }
    }


    getchar();
    getchar();
    return 0;
}


猜你喜欢

转载自blog.csdn.net/wang_huizhang/article/details/78152714