P2010 palindrome date

    Uh, uh, the code I wrote for this question is relatively cumbersome, but I think it is still easy to understand.

    The input is two dates, if you think of it as a number, the data range is 10000000-99999999; and many of the numbers will not appear (must be the actual date) if it is from the first input If you count to the second number and cycle once, the time limit of 1 s can also be passed. During the cycle, you must determine whether the number is a palindrome, and then determine whether the number is a legal date. So just define two functions and judge separately.

    The first is a function to determine whether the date is legal:

 1 bool hf(int a)
 2 {
 3     int t=a,date,year;
 4     year=t/10000;
 5     t=t%10000;
 6     date=t%100;
 7     t=t/100;
 8     if(t<1 || t>12) return false;
 9     if(t==1||t==3||t==5||t==7||t==8||t==10||t==12)
10     {
11         if(date>31 || date<1) return false;
12         else return true;
13     }
14     if(t==4||t==6||t==9||t==11)
15     {
16         if(date>30 || date<1) return false;
17         else return true;
18     }
19     if(t==2)
20     {
21         if((year%4==0 && year%100!=0) || year%400==0)
22         {
23             if(date>29 || date<1) return false;
24             else return true;
25         }
26         else
27         {
28             if(date>28 || date<1) return false;
29             else return true;
30         }
31     }
32 }

        The return value only needs to be "true" or "false", so the function type is bool, and the function to judge the palindrome is the same. Three variables: year to store the year, t to store the month, and date to store the date. After saving them separately, first judge the month. If the month is illegal, return false directly; and then judge whether it is legal according to the maximum number of days corresponding to each month. February is quite special, we must first determine whether it is a leap year or an ordinary year, and then determine whether the date is legal.

        Then, determine whether it is a function of palindrome:

1 bool hw(int a)
2 {
3     if(a/10000000==a%10 && a/1000000%10==a%100/10 && a/100000%10==a%1000/100 && a/10000%10==a%10000/1000)
4     {
5         return true;
6     }
7     else return false; 
8 }

        When judging the palindrome, directly flip the last four digits, and then judge whether it is equal to the first four digits. If they are equal, it is the number of palindromes.

       Then just loop and call the function in the main function.

       AC code:

 1 #include<iostream>
 2 using namespace std;
 3 bool hf(int a)
 4 {
 5     int t=a,date,year;
 6     year=t/10000;
 7     t=t%10000;
 8     date=t%100;
 9     t=t/100;
10     if(t<1 || t>12) return false;
11     if(t==1||t==3||t==5||t==7||t==8||t==10||t==12)
12     {
13         if(date>31 || date<1) return false;
14         else return true;
15     }
16     if(t==4||t==6||t==9||t==11)
17     {
18         if(date>30 || date<1) return false;
19         else return true;
20     }
21     if(t==2)
22     {
23         if((year%4==0 && year%100!=0) || year%400==0)
24         {
25             if(date>29 || date<1) return false;
26             else return true;
27         }
28         else
29         {
30             if(date>28 || date<1) return false;
31             else return true;
32         }
33     }
34 }
35 bool hw(int a)
36 {
37     if(a/10000000==a%10 && a/1000000%10==a%100/10 && a/100000%10==a%1000/100 && a/10000%10==a%10000/1000)
38     {
39         return true;
40     }
41     else return false; 
42 }
43 int main()
44 {
45     int m,n,s=0;
46     cin>>m>>n;
47     for(int i=m;i<=n;++i)
48     {
49         if(hf(i) && hw(i))
50         {
51             s++;
52         }
53     }
54     cout<<s;
55     return 0;
56 }

Guess you like

Origin www.cnblogs.com/zkw666/p/12722352.html