题目:
题解:
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
string x, y; //转化前,转化后
char key;
cin >> key;
getchar(); //读取回车
getline(cin, x);
//判断要压缩还是解压
if (key == 'C') //压缩
{
int count_1 = 1; //记录每一个重复的字符
for (int i = 0; i < (int)x.size(); i++)
{
if (x[i] == x[i + 1])
{
count_1++;
continue;
}
else if (x[i] != x[i + 1])
{
//没有重复的原样拷贝
if (count_1 == 1)
{
y += x[i];
}
else //有重复的压缩处理
{
y += to_string(count_1); //拷贝数字
y += x[i]; //拷贝字符
count_1 = 1;
}
}
}
}
else if (key == 'D') //解压
{
int count_2 = 0; //读取到数字的个数
int num = 0; //用于接收字符转化后的值
for (int i = 0; i < (int)x.size(); i++)
{
//读取到字符个数
if (isdigit(x[i]))
{
num = num*10+(x[i] - '0');
count_2++;
if (!isdigit(x[i + 1]))
{
//拷贝字母
while (num--)
{
y += x[i + 1];
}
i++;
num = 0;
count_2 = 0;
}
}
else //读取到字符原样拷贝
{
y += x[i];
continue;
}
}
}
cout << y;
return 0;
}
标签:count,PAT,int,乙级,else,++,num,拷贝,刷题
From: https://blog.csdn.net/2301_79580018/article/details/139158400