<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 一.題目描述 The count-and-say sequence is the sequence of integers beginning as follows:? 1, 11, 21, 1211, 111221, …? 1 is read off as “one 1” or 11.? 11 is read off as “two 1s” or 21.? 21 is read off as “one 2”, then “one 1” or 1211.? Given an integer n, generate the nth sequence.? Note: The sequence of integers will be represented as a string. ## 二.題目分析 題目的內容很多,其實就是根據一個數的讀法,組合出下一個數,比如11,讀作2(個)1,因此下個數是21;同理,21讀作1(個)2、1(個)1,因此下個數是1211… 根據題意,發現該題沒有什么高級的技巧,代碼中主要使用了`string`和`stringstream`,需要加入頭文件`#include <string>`,`#include <sstream>`,關于`string`和`stringstream`的用法,可參考:[http://blog.csdn.net/xw20084898/article/details/21939811](http://blog.csdn.net/xw20084898/article/details/21939811)?。該程序只是實現了對題目要求的模擬,然后輸出結果。 ## 三.示例代碼 ~~~ #include <string> #include <sstream> using namespace std; class Solution { public: string CountAndSay(int n) { string result = "1"; while (--n) result = theNextStr(result); return result; } private: string theNextStr(const string& str) { if (str.empty()) return string(); stringstream result; int strSize = str.size(); int count = 1; // 計數 for (int Index = 0; Index < strSize - 1; Index++) { if (str[Index] == str[Index + 1]) count++; else { result << count << str[Index]; count = 1; } } result << count << str[strSize - 1]; // 最后一位 return result.str(); } }; ~~~ ![](https://box.kancloud.cn/2016-01-05_568bb5ec18df4.jpg) ## 四.小結 需要好好學習一下`string`和`stringstream`的用法。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看