一、题目
二、解题思路
- 遍历输入的字符串;
- 当遇到 6 时,cot 赋初值为 1 ,循环后面的字符(包括字符串的结束标志 '\0' ,不然当字符串的最后一个字符为 6 时,不会进入 else 进行输出):当为 6 时,cot++ ,否则根据 cot 的值输出对应的 27,9,6 ;
- 不是 6 则直接输出。
三、代码
#include<iostream>
using namespace std;
#include<string>
int main()
{
string str;
getline(cin,str);
int len = str.length();
for(int i=0;i<len;i++)
{
if(str[i]=='6')
{
int cot=1;
// for(int j=i+1;j<=len;j++)
for(int j=i+1;;j++)
{
if(str[j]=='6')
{
cot++;
}
else
{
if(cot>9)
{
cout<<"27";
}
else if(cot>3)
{
cout<<"9";
}
else
{
while(cot--)
{
cout<<"6";
}
}
i=j-1;
break;
}
}
}
else
{
cout<<str[i];
}
}
return 0;
}
四、总结
注意字符串最后一个字符为 6 的情况。