当前位置:   article > 正文

第十四届蓝桥杯填空题(日期统计)

第十四届蓝桥杯填空题(日期统计)

问题描述

小蓝现在有一个长度为  100 的数组,数组中的每个元素的值都在  0 到  9 的范围之内。数组中的元素从左至右如下所示:

5 6 8 6 9 1 6 1 2 4 9 1 9 8 2 3 6 4 7 7 5 9 5 0 3 8 7 5 8 1 5 8 6 1 8 3 0 3 7 9 2 7 0 5 8 8 5 7 0 9 9 1 9 4 4 6 8 6 3 3 8 5 1 6 3 4 6 7 0 7 8 2 7 6 8 9 5 6 5 6 1 4 0 1 0 0 9 4 8 0 9 1 2 8 5 0 2 5 3 3

现在他想要从这个数组中寻找一些满足以下条件的子序列


1.子序列的长度为 8

2.这个子序列可以按照下标顺序组成一个  yyyymmdd 格式的日期,并且要求这个日期是  2023 年中的某一天的日期,例如  20230902, 20231223。 yyyy 表示年份, mm 表示月份,dd 表示天数,当月份或者天数的长度只有一位时需要一个前导零补充。

请你帮小蓝计算下按上述条件一共能找到多少个不同的 2023 年的日期。对于相同的日期你只需要统计一次即可。

答案提交

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

//从第一个2开始就行,所以长度为92

  1. import java.util.*;
  2. import java.math.*;
  3. public class Main{
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. int[] a=new int[92];
  7. for(int i=0;i<92;i++){
  8. a[i]=scan.nextInt();
  9. }
  10. HashSet<Integer> set=new HashSet<>();
  11. for(int a1=0;a1<=84;a1++) {
  12. for(int a2=a1+1;a2<=85;a2++) {
  13. for(int a3=a2+1;a3<=86;a3++) {
  14. for(int a4=a3+1;a4<=87;a4++) {
  15. int year=a[a1]*1000+a[a2]*100+a[a3]*10+a[a4];
  16. if(year==2023) {
  17. for(int a5=a4+1;a5<=88;a5++) {
  18. for(int a6=a5+1;a6<=89;a6++) {
  19. int month=a[a5]*10+a[a6];
  20. if(month>=1&&month<=12) {
  21. for(int a7=a6+1;a7<=90;a7++) {
  22. for(int a8=a7+1;a8<=91;a8++) {
  23. int day=a[a7]*10+a[a8];
  24. if(day>=1&&day<=31) {
  25. if(check(year,month,day)) {
  26. int num=(year*100+month)*100+day;
  27. set.add(num);
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
  40. System.out.println(set.size());
  41. scan.close();
  42. }
  43. public static boolean check(int year,int month,int day){
  44. if(month==2&&day<=28){
  45. return true;
  46. }
  47. if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
  48. if(day>=1&&day<=31){
  49. return true;
  50. }
  51. else{
  52. return false;
  53. }
  54. }
  55. if(month==4||month==6||month==9||month==11){
  56. if(day>=1&&day<=30){
  57. return true;
  58. }
  59. else{
  60. return false;
  61. }
  62. }
  63. return false;
  64. }
  65. }

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

闽ICP备14008679号