AcWing 2066. 解码
原题链接
简单
作者:
诺亚方舟.
,
2021-02-02 15:42:18
,
所有人可见
,
阅读 388
字符个数不超过9
#include <iostream>
using namespace std;
string str, ans;
int main()
{
cin >> str;
for (int i = 0; i < str.size(); )
{
char c = str[i];
int j = i + 1, cnt = 1;
if ('0' <= str[j] && str[j] <= '9')
{
cnt = str[j] - '0';
j++;
}
while (cnt--) ans += c;
i = j;
}
cout << ans << endl;
return 0;
}
若考虑字符个数超过9的情况
#include <iostream>
using namespace std;
string str, ans;
int main()
{
cin >> str;
for (int i = 0; i < str.size(); )
{
char c = str[i];
int j = i + 1, sum = 0;
while ('0' <= str[j] && str[j] <= '9' && j < str.size())
{
sum = sum * 10 + str[j] - '0';
j++;
}
if (sum) while (sum--) ans += c;
else ans += c;
i = j;
}
cout << ans << endl;
return 0;
}