{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "c6f46d0c1b2f482fa29cf08bd6cf0215",
  "keywords": "字符串",
  "title": "外观数列",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">\n<p>给定一个正整数 <code>n</code> ，输出外观数列的第 <code>n</code> 项。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>「外观数列」是一个整数序列，从数字 1 开始，序列中的每一项都是对前一项的描述。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>你可以将其视作是由递归公式定义的数字字符串序列：</p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>countAndSay(1) = \"1\"</code></li>\n<li><code>countAndSay(n)</code> 是对 <code>countAndSay(n-1)</code> 的描述，然后转换成另一个数字字符串。</li>\n</ul>",
      "language": "markdown"
    },
    {
      "content": "\n<p>前五项如下：</p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre>\n1.     1\n2.     11\n3.     21\n4.     1211\n5.     111221\n第一项是数字 1 \n描述前一项，这个数是 1 即 “ 一 个 1 ”，记作 \"11\"\n描述前一项，这个数是 11 即 “ 二 个 1 ” ，记作 \"21\"\n描述前一项，这个数是 21 即 “ 一 个 2 + 一 个 1 ” ，记作 \"1211\"\n描述前一项，这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ，记作 \"111221\"\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>要 <strong>描述</strong> 一个数字字符串，首先要将字符串分割为 <strong>最小</strong> 数量的组，每个组都由连续的最多 <strong>相同字符</strong>\n组成。然后对于每个组，先描述字符的数量，然后描述字符，形成一个描述组。要将描述转换为数字字符串，先将每组中的字符数量用数字替换，再将所有描述组连接起来。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>例如，数字字符串 <code>\"3322251\"</code> 的描述如下图：</p>\n<img alt=\"\"\nsrc=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0038.Count%20and%20Say/images/countandsay.jpg\"\nstyle=\"width: 581px; height: 172px;\" />\n<ul>\n</ul>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 1：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>n = 1\n<strong>输出：</strong>\"1\"\n<strong>解释：</strong>这是一个基本样例。\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 2：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>n = 4\n<strong>输出：</strong>\"1211\"\n<strong>解释：</strong>\ncountAndSay(1) = \"1\"\ncountAndSay(2) = 读 \"1\" = 一 个 1 = \"11\"\ncountAndSay(3) = 读 \"11\" = 二 个 1 = \"21\"\ncountAndSay(4) = 读 \"21\" = 一 个 2 + 一 个 1 = \"12\" + \"11\" = \"1211\"\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>提示：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>1 &lt;= n &lt;= 30</code></li>\n</ul>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic void parse(char *input, char *output)\n{\n\tchar *p = input;\n\tchar *q = output;\n\twhile (*p != '\\0')\n\t{\n\t\tint count = 1;\n\t\twhile (p[0] == p[1])\n\t\t{\n\t\t\tcount++;\n\t\t\tp++;\n\t\t}\n\t\tint n = 0;\n\t\twhile (count > 0)\n\t\t{\n\t\t\tn += count % 10;\n\t\t\tcount /= 10;\n\t\t}\n\t\twhile (n > 0)\n\t\t{\n\t\t\t*q++ = (n % 10) + '0';\n\t\t\tn /= 10;\n\t\t}\n\t\t*q++ = p[0];\n\t\tp++;\n\t}\n\t*q = '\\0';\n}\nstatic char *countAndSay(int n)\n{\n\tif (n < 1)\n\t{\n\t\treturn NULL;\n\t}\n\tchar *result;\n\tchar *prev = malloc(10000);\n\tchar *next = malloc(10000);\n\tstrcpy(prev, \"1\");\n\tif (n == 1)\n\t{\n\t\treturn prev;\n\t}\n\tint i;\n\tfor (i = 2; i <= n; i++)\n\t{\n\t\tif (i & 0x1)\n\t\t{\n\t\t\tparse(next, prev);\n\t\t\tresult = prev;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tparse(prev, next);\n\t\t\tresult = next;\n\t\t}\n\t}\n\treturn result;\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test n\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%s\\n\", countAndSay(atoi(argv[1])));\n\treturn 0;\n}",
    "language": "cpp"
  },
  "node_id": "dailycode-476afc7e273347f9bd276d7b148cf931",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470795"
}