赞
踩
代码:
- /*
- (year % 4 == 0 && year % 100) || (year % 400 == 0)
- 是闰年:
- 1.整除4但不整除100
- 2.整除400
- 闰年和平年的差别在第2月:
- 闰年的第二月有29天,平年的第二月28天
- */
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cstdio>
- #define N 5
- using namespace std;
-
- int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
-
- int check_vaild(int n)
- {
- int year = n / 10000 , month = n % 10000 / 100 , day = n % 100;
-
- if(month < 0 || month > 12) return false;//月份超范围
-
- if(day < 0 || (day > days[month] && month != 2)) return false;//先不判断第二个月的天数是否超出范围
-
- //单独判断第二个月的天数:分为闰年和平年
- int x = 0;
- if(month == 2)
- x = year % 4 == 0 && year % 100 || year % 400 == 0;
- if(day > days[month] + x) return false;
-
- return true;
- }
-
- int check_ABAB(int n)
- {
- int a = n / 10000000 , b = n / 1000000 % 10 , c = n /100000 % 10 , d = n / 10000 % 10;
- if(a == c && b == d && a != b) return true;
- return false;
- }
-
- int main()
- {
- int n;
- scanf("%d",&n);
- /*不知道下一个回文日期的年份(即前四位数),
- 就把前四位数作为一个整体枚举,枚举的时候同时把回文串拼接在后面存储在x里
- */
- int falg = 1;
- for(int i = n / 10000;i < 100000;i ++)//i是前四位数
- {
- int x = i , t = i;
- for(int j = 0;j < 4;j ++)
- x = x*10 + t%10 , t /= 10;//2020 * 10 + (2020) % 10 = 20200 把t的最后一位数去掉
- if(check_vaild(x) && x > n && falg)
- {
- printf("%d\n",x);
- falg = 0;//找到下一个回文串后,标志结束
- }
- if(check_vaild(x) && check_ABAB(x) && x > n)
- {
- printf("%d\n",x);
- break;
- }
- }
-
-
- return 0;
- }
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。