一直都不太会字符串,字符串凉凉
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int main() {
int cnt=0;///连续的6的个数
int k=0;///字符串的长度
char s[maxn], ch;
while(scanf("%c", &ch), ch!='\n') s[k++]=ch;
s[k]='\0';
/// PTA不支持gets,当然读取一行的字符串的时候,还可用C++的getline等,记得加头文件 #include<string>哦。
/// 遍历字符串的时候,遍历到字符串的结束符,保证cnt在遍历完之后必定为0
for(int i=0; i<=k; i++) {
if(s[i]=='6') cnt++;
else {
if(cnt>9) printf("27");
else if(cnt>3) printf("9");
else while(cnt--) printf("6");
cnt=0;
if(i==k) continue;
printf("%c", s[i]);
}
}
return 0;
}
秀操作??!
正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
getline(cin, s);
cout << regex_replace(regex_replace(s, regex("6{10,}"), "27"), regex("6{4,}"), "9");
return 0;
}