AC代码(C++):
class Solution {
public:
string countAndSay(int n) {
string a="1";
while(-- n){
string ans;
int cnt = 0;
char c = a[0];
for(int i=0; i<a.length(); ++i){
if(a[i]==c)
cnt ++;
else{
ans += cnt+'0';
ans += c;
c = a[i];
cnt = 1;
}
}
if(cnt > 0){
ans += cnt+'0';
ans += c;
}
a = ans;
}
return a;
}
};