# 20. 报数

java

```java
/**
 * 报数序列是指一个整数序列，按照其中的整数的顺序进行报数，得到下一个数。其前五项如下：
 *
 * 1.     1
 * 2.     11
 * 3.     21
 * 4.     1211
 * 5.     111221
 * 1 被读作  "one 1"  ("一个一") , 即 11。
 * 11 被读作 "two 1s" ("两个一"）, 即 21。
 * 21 被读作 "one 2",  "one 1" （"一个二" ,  "一个一") , 即 1211。
 *
 * 给定一个正整数 n ，输出报数序列的第 n 项。
 */
public class Solution38 {

    /**
     * 这道题重要的是要理解题意，题目有点难懂
     * 第一个序列为1，之后的序列都是由前一个递推出来的
     * 递推规则如下:
     * 假设上一个序列为AABCDD,那么该序列为2A1B1B2D
     * 统计序列中连续的字符的个数以及字符，拼接起来
     * AA就是2A，B就是1B，C就是1C，DD就是2D
     *
     * 接下来只要根据式子递推就可以了
     *
     * @param n
     * @return
     */
    public String countAndSay(int n) {
        int i=1;
        String now="1";
        while(i<n){
            now=next(now);
            i++;
        }
        return now;
    }

    /**
     * 推导v的下一个序列
     * @param v
     * @return
     */
    public String next(String v) {
        if(v==null||v.length()==0) return null;

        String r="";//序列
        char pre=v.charAt(0);//重复的字符
        int c=1;//重复个数

        for(int i=1;i<v.length();i++){
            char ch=v.charAt(i);
            if(ch==pre){
                c++;
            }else{
                r+=c+""+(pre-'0');
                pre=ch;
                c=1;
            }
        }
        r+=c+""+(pre-'0');
        return r;
    }

    public static void main(String[] args) {
        int n = 5;
        String r = new Solution38().countAndSay(n);
        System.out.println(r);
    }
}
```

python

```python
def countAndSay(n):
    if n == 0:
        return ''
    res = '1'
    while n != 0:
        n -= 1
        print(res)
        i = 0
        count = 1
        cur = ''
        # 拼接res的读数
        while i < len(res):
            count = 1
            while i + 1 < len(res) and res[i] == res[i + 1]:
                count += 1
                i += 1
            #     数量+本身
            cur += str(count) + res[i]
            # 到下一个数字位
            i += 1
        res = cur
    return res


s = countAndSay(6)
'''
1
11
21
1211
111221
312211
'''
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/zhi-shang-lei/20-bao-shu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
