蓝桥杯 day01 奇怪的数列 特殊日期

发布时间:2023年12月18日

奇怪的数列

题目描述

奇怪的数列

从 X 星截获一份电码,是一些数字,如下:

13

1113

3113

132113

1113122113

??

YY 博士经彻夜研究,发现了规律:

第一行的数字随便是什么,以后每一行都是对上一行"读出来"

比如第 2 行,是对第 1 行的描述,意思是:1 个 1,1 个 3,所以是:1113

第 3 行,意思是:3 个 1,1 个 3,所以是:3113

请你编写一个程序,可以从初始数字开始,连续进行这样的变换。

输入描述

第一行 输入一个数字组成的串,不超过 100 位。

第二行,一个数字?n,表示需要你连续变换多少次,n?不超过 20。

输出描述

输出一个串,表示最后一次变换完的结果。

输入输出样例

示例

输入?

5
7

输出

13211321322115

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 512M

我的解答:

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {

    /**
     * 计算传入字串第一个字符连续出现的个数
     */
    private static int countNum(String s){
      int cnt=1;
      // 获取字符串第一个字符
      char c = s.charAt(0);
      for(int i=1;i<s.length();i++){
        if(c == s.charAt(i)){
          cnt++;
        }else{
          break;
        }
      }
      return cnt;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        // 初始化结果字符串
        String result = new String();
        // m 不超过100
        String m = scan.next();
        // n 不超过 20
        int n = scan.nextInt();
        // 经过 n 次变换
        for(int i=1;i<=n;i++){
          StringBuilder temp = new StringBuilder();
          for(int j=0;j<m.length();){
            // 需要处理的子串
            String subString = m.substring(j);
            // 拿到当前数字
            char subChar = subString.charAt(0);
            // 拿到m中前面数字的长度
            int cnt = countNum(subString);
            // 追加当前数字出现的个数
            temp.append(cnt);
            // 追加当前数字
            temp.append(subChar);
            j+=cnt;
          }
          m = temp.toString();
          result = m;
        }
        System.out.println(result);
        scan.close();
    }
}

特殊日期

问题描述

对于一个日期,我们可以计算出年份的各个数位上的数字之和,也可以分别计算月和日的各位数字之和。请问从?19001900?年?11?月?11?日至?99999999?年?1212?月?3131?日,总共有多少天,年份的数位数字之和等于月的数位数字之和加日的数位数字之和。

例如,20222022?年?1111?月?1313?日满足要求,因为?2+0+2+2=(1+1)+(1+3)2+0+2+2=(1+1)+(1+3)?。

请提交满足条件的日期的总数量。

答案提交

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

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 256M
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    /**
     * 通过 month 获取日期
     */
    private static int getDayByMonth(int year,int month){
        // 二月的天数
        int febDay;
        febDay = (year%4==0 && year%100!=0 || year%400==0)? 29 : 28;
        switch(month){
            case 1: 
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
              return 31;
            case 4:
            case 6:
            case 9:
            case 11:
              return 30;
            case 2:
             return febDay;
        }
        return febDay;

    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int cnt=0;
        //在此输入您的代码...
        for(int year=1900;year<9999;year++){
            for(int month=1;month<=12;month++){
                for(int day=1;day<=getDayByMonth(year,month);day++){
                    if(year/1000 + year%1000/100 + year%100/10 + year%10 == month/10 + month%10 + day/10 + day%10 ){
                        cnt++;
                    }
                }
            }
        }
        System.out.println(cnt);
        scan.close();
    }
}

感觉挺简单的,没什么难度。

文章来源:https://blog.csdn.net/m0_46745664/article/details/134891440
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。