这题也很简单,主要是通过这题学习字符串和数字之间的转换。
数字转换为string用to_string,例如
string s = to_string(num)
string转换为int使用stoi,例如
int num = stoi(s)
string转换为double使用stod,例如
int num = stod(s)
最后,是这题的代码:
#include <bits/stdc++.h>
using namespace std;
int d, n;
int main()
{
cin >> d >> n;
string s = "";
s += (d + '0');
for (int i = 1; i < n; i++) {
string ss = "";
for (int j = 0; j < s.size(); j++) {
char c = s[j];
int now = j, cnt = 1;
while (now + 1 < s.size() && s[now + 1] == c) now++, cnt++;
ss += c;
ss += to_string(cnt);
j = now;
}
s = ss;
}
cout << s;
return 0;
}