序列1,11,21,1211,111221

1. 序列1,11,21,1211,111221,_,下一个数是什么?

答:312211

  • 1 读为: 1个1,提取数字11(即为第二个数);
  • 11 读为: 2个1,提取数字21(即为第三个数);
  • 21 读为: 1个2、1个1,提取数字1211(即为第四个数);
  • 1211 读为:1个1、1个2、2个1;提取数字111221(即为第四个数);
2. 编程实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Demo {
public static void main(String[] args) {
int count = 6;
String[] str = new String[count];
str[0] = "1";
str[1] = "11";
int j;
StringBuilder result;
for (int i = 2; i < count; i++) {
char[] pri = str[i - 1].toCharArray();
int n;
result = new StringBuilder();
j = 0;
for (n = 0; n < pri.length - 1; n++) {
if (pri[n] == pri[n + 1]) {
j++;
} else {
j++;
result.append(j);
result.append(pri[n]);
j = 0;
}
}
j++;
result.append(j);
result.append(pri[n]);
str[i] = result.toString();
}
for (int i = 1; i <= count; i++) {
System.out.println(i + "个数: " + str[i-1]);
}
}
}