目录
一【题目描述】
二【题目样例】
三【解题思路】
输入时注意:
①:一定要用getchar()读取换行符,再用gets()读取字符串
因为读入scanf()读入C或D时,不读取换行符,而又输入换行符。
若不用getchar()读取 ,直接用gets(),gets()遇到换行符直接结束
有以下两个情况:
①:压缩,借助 while()
②:第二种情况就是要解压,只要扫描到数字就记录下来,然后按照个数打印字母即可,但是需要注意,数字的个数可能超过1位,所以可能有两位数甚至三位数的可能,
其中有两个坑点:
四【代码实现】
#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
using namespace std;
int main(){
char x,str[1001];
scanf("%c",&x);
getchar();//吸收换行符
gets(str);
if(x=='C'){//压缩
int i=0;
for(int i=0;i<strlen(str);i++){
int count=1;
while(str[i]==str[i+1]){
count++;
i++;
}
if(count>1) printf("%d",count);
printf("%c",str[i]);
i++;
}
}
if(x=='D'){//解压
int i=0;
for(int i=0;i<strlen(str);i++){
int count=0;
while(str[i]>='0'&&str[i]<='9'){
count=count*10+str[i]-'0';
i++;
}
if(count==0) printf("%c",str[i]);
else{
while(count--){
printf("%c",str[i]);
}
}
i++;
} //
}
}