当前位置:   article > 正文

第十一届蓝桥杯省赛C++ 试题G:回文日期【代码详细注释版】_回文日期【第十一届】

回文日期【第十一届】

代码:

  1. /*
  2. (year % 4 == 0 && year % 100) || (year % 400 == 0)
  3. 是闰年:
  4. 1.整除4但不整除100
  5. 2.整除400
  6. 闰年和平年的差别在第2月:
  7. 闰年的第二月有29天,平年的第二月28天
  8. */
  9. #include<iostream>
  10. #include<cstring>
  11. #include<algorithm>
  12. #include<cstdio>
  13. #define N 5
  14. using namespace std;
  15. int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
  16. int check_vaild(int n)
  17. {
  18. int year = n / 10000 , month = n % 10000 / 100 , day = n % 100;
  19. if(month < 0 || month > 12) return false;//月份超范围
  20. if(day < 0 || (day > days[month] && month != 2)) return false;//先不判断第二个月的天数是否超出范围
  21. //单独判断第二个月的天数:分为闰年和平年
  22. int x = 0;
  23. if(month == 2)
  24. x = year % 4 == 0 && year % 100 || year % 400 == 0;
  25. if(day > days[month] + x) return false;
  26. return true;
  27. }
  28. int check_ABAB(int n)
  29. {
  30. int a = n / 10000000 , b = n / 1000000 % 10 , c = n /100000 % 10 , d = n / 10000 % 10;
  31. if(a == c && b == d && a != b) return true;
  32. return false;
  33. }
  34. int main()
  35. {
  36. int n;
  37. scanf("%d",&n);
  38. /*不知道下一个回文日期的年份(即前四位数),
  39. 就把前四位数作为一个整体枚举,枚举的时候同时把回文串拼接在后面存储在x里
  40. */
  41. int falg = 1;
  42. for(int i = n / 10000;i < 100000;i ++)//i是前四位数
  43. {
  44. int x = i , t = i;
  45. for(int j = 0;j < 4;j ++)
  46. x = x*10 + t%10 , t /= 10;//2020 * 10 + (2020) % 10 = 20200 把t的最后一位数去掉
  47. if(check_vaild(x) && x > n && falg)
  48. {
  49. printf("%d\n",x);
  50. falg = 0;//找到下一个回文串后,标志结束
  51. }
  52. if(check_vaild(x) && check_ABAB(x) && x > n)
  53. {
  54. printf("%d\n",x);
  55. break;
  56. }
  57. }
  58. return 0;
  59. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/385417
推荐阅读
相关标签
  

闽ICP备14008679号