西电复试之——真题2011C字符压缩算法
输入:a5b3aba13b4
输出:aaaaabbbabaaaaaaaaaaaaabbbb
#include<iostream>
#include<string>
using namespace std;
int main() {
string str;
cin >> str;
int len = str.length();
char c;//记录当前字符
for (int i = 0; i < len; i++) {
int temp = 0;
//登记字符
if ((str[i]) < '0' || (str[i]) > '9') {
c = str[i];
cout << c;
}
//记录倍数
if ((str[i]) >= '0' && (str[i]) <= '9') {
temp += str[i] - '0';
i++;
while ((str[i]) >= '0' && (str[i]) <= '9' && i < len) {
temp = temp * 10 + str[i] - '0';
i++;
}
i--;
for (int j = 1; j < temp; j++)
cout << c;
}
}
return 0;
}