代码
#include <iostream>
using namespace std;
int main() {
string d;
int N;
cin>>d>>N;
while(--N){
string ans;
char c = d[0];
int count = 0;
for(int i=0; i<d.length(); i++){
if(d[i]==c){
count++;
}
else{
ans += c;
ans += count + '0';
c = d[i];
count = 1;
}
}
if(count>0){
ans += c;
ans += count + '0';
}
d = ans;
}
cout<<d<<endl;
return 0;
}
注解
1、注意–N与N–的区别
2、用char保留当前的字符,count来计数。如果遇到相同字符,count++即可,如果遇到不同字符,首先保存字符和个数。然后用当前字符充当新结果。最后不要忘记加上结尾的那些字符和个数。