題目描述
從 X 星截獲一份電碼,是一些數字,如下:
13
1113
3113132113
1113122113
?
YY 博士經徹夜研究,發現了規律:
第一行的數字隨便是什么,以后每一行都是對上一行"讀出來"
比如第 2 行,是對第 1 行的描述,意思是:1 個 1,1 個 3,所以是:1113
第 3 行,意思是:3 個 1,1 個 3,所以是:3113
請你編寫一個程序,可以從初始數字開始,連續進行這樣的變換。
輸入描述
第一行輸入一個數字組成的串,不超過 100 位。
第二行,一個數字? n,表示需要你連續變換多少次, n?不超過 20。
輸出描述
輸出一個串,表示最后一次變換完的結果。
輸入輸出樣例
示例
輸入
5
7
輸出
13211321322115
運行限制
import java.util.*;
public class Main{public static void main(String[] args) {Scanner scan = new Scanner(System.in);String str=scan.next();int n=scan.nextInt();String ans=str;while(n>0) {n--;ans=f(ans);}System.out.println(ans);}public static String f(String s) {int count=1;String y="";int t=s.charAt(0)-'0';for(int i=1;i<s.length();i++) {int p=s.charAt(i)-'0';if(t==p) {count++;}else {y+=count;y+=t;count=1;t=p;}}y+=count;y+=t;return y;}
}
-
- 最大運行時間:1s
- 最大運行內存: 512M