代码:
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 5 using namespace std; 6 7 8 string countAndSay(int n) 9 { 10 string s = "1"; 11 int i = 0; 12 while (--n > 0) 13 { 14 char c = s[0]; 15 string b = ""; 16 i = 0; 17 while (i < s.length()) 18 { 19 i++; 20 int count = 1; 21 while (i<s.length()&&s[i] == c) 22 { 23 count++; 24 i++; 25 } 26 b = b + (char)(count + '0'); 27 b = b + c; 28 if (i == s.length()) 29 { 30 break; 31 } 32 c = s[i]; 33 } 34 s = b; 35 } 36 return s; 37 } 38 39 int main() 40 { 41 cout << countAndSay(2) << endl; 42 }