Leetcode-38. 外观数列
题目:
代码:
#include <iostream>
using namespace std;
string countAndSay(int n) {
string str[n];
int count=1;
str[0]="1";
if(n==1){
return str[0];
}
for(int i=0;i<n-1;i++){
str[i+1]="";
for(int j=0;j<str[i].size()-1;j++){
if(str[i][j]==str[i][j+1]){
count++;
}else{
str[i+1]+=count+'0';
str[i+1]+=str[i][j];
count=1;
}
}
str[i+1]+=count+'0';
str[i+1]+=str[i][str[i].size()-1];
count=1;
}
return str[n-1];
}
int main(){
string s;
int n;
cin>>n;
s=countAndSay(n);
cout<<s;
return 0;
}