大家好,我是陈景序,今天来跟大家聊聊一个有趣的小问题:如何在字符串中连续的6替换成9或者27呢?
问题背景
“666”这个网络用语大家应该都不陌生,它表示某人很厉害或者我们很佩服的意思。最近,这个表达方式又升级了,变成了“9”和“27”,意思是“6翻了”和“3个9”,听起来是不是很厉害?
问题解析
这个问题的核心是将输入的字符串中连续的6替换成9或者27。具体规则如下:
- 如果连续的6的个数超过3个但不超过9个,则替换成9。
- 如果连续的6的个数超过9个,则替换成27。
- 其他字符保持不变。
代码实现
下面是一个C语言的实现示例:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
int main() {
char list[1001] = { 0 };
gets(list);
int leng = strlen(list);
for (int i = 0; i < leng; i++) {
int cout = 0;
if (list[i] - '0' == 6) {
int start_p = i;
int j = i;
while (1) {
cout++;
j++;
if (list[j] - '0' == 6) {
;
}
else {
break;
}
}
if (cout > 3 && cout <= 9) {
list[start_p] = '9';
for (int x = start_p + 1; x < start_p + cout; x++) {
list[x] = '@';
}
i = start_p + cout;
}
else if (cout > 9) {
list[start_p] = '2';
list[start_p + 1] = '7';
for (int x = start_p + 2; x < start_p + cout; x++) {
list[x] = '@';
}
i = start_p + cout;
}
}
}
for (int i = 0; i < leng; i++) {
if (list[i] == '@') {
;
}
else {
printf(
