蓝桥杯题目

[蓝桥杯]奇怪的数列

2020-01-30  本文已影响0人  二十五六岁的你

问题 1833: [蓝桥杯][2015年第六届真题]奇怪的数列

题目描述

从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
package 字符串;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: 76147
 * Date: 2020-01-29
 * Time: 17:29
 * Description:
 */
public class 奇怪的数列 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.next();
            int n = sc.nextInt();

            int q = 0;
            for (int i = 0; i < n; i++) {
                int num = 1;
                String temp = "";
                for (int j = 0; j < str.length(); j++) {
                    if (str.length() == 1) {
                        temp = 1 + str;
                    } else {
                        if (j + 1 < str.length() && str.charAt(j) == str.charAt(j + 1)) {
                            num++;
                            continue;
                        }
                        temp += num + "" + (str.charAt(j) - '0');
                        num = 1;
                    }
                }
                str = temp;
            }
            System.out.println(str);
        }
    }

}

上一篇 下一篇

猜你喜欢

热点阅读