西电复试之——真题2011C字符压缩算法

西电复试之——真题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;
}
原创文章 35 获赞 17 访问量 1253

猜你喜欢

转载自blog.csdn.net/qq_41436493/article/details/105834116
今日推荐