0
点赞
收藏
分享

微信扫一扫

团体程序设计天梯赛-练习集L1-058 6翻了 (15 分)

凯约 2022-04-21 阅读 64

文章目录


前言

“666”是一种网络用语,大概是表示某人很厉害、我们很佩服的意思。最近又衍生出另一个数字“9”,意思是“6翻了”,实在太厉害的意思。如果你以为这就是厉害的最高境界,那就错啦 —— 目前的最高境界是数字“27”,因为这是 3 个 “9”!

本题就请你编写程序,将那些过时的、只会用一连串“6666……6”表达仰慕的句子,翻译成最新的高级表达。

输入格式:
输入在一行中给出一句话,即一个非空字符串,由不超过 1000 个英文字母、数字和空格组成,以回车结束。

输出格式:
从左到右扫描输入的句子:如果句子中有超过 3 个连续的 6,则将这串连续的 6 替换成 9;但如果有超过 9 个连续的 6,则将这串连续的 6 替换成 27。其他内容不受影响,原样输出。

输入样例:

it is so 666 really 6666 what else can I say 6666666666

输出样例:

it is so 666 really 9 what else can I say 27

一、C语言解答

#include <stdio.h>
#define MAX 1001

int main()
{
	char a[MAX], *q;
	int count = 0;
	gets(a);
	q = a;
	while (*q != '\0')
	{
		if (*q != '6')
		{
			printf("%c", *q);
			q++;
		}
		else
		{
			count = 0;
			while (*q != '\0' && *q == '6')
			{
				count++;
				q++;
			}
			
			if (count > 9)
			{
				printf("%s", "27");
			}
			else if (count > 3)
			{
				printf("%c", '9');
			}
			else if (count == 3)
			{
				printf("%c%c%c", '6', '6', '6');
			}
			else if (count == 2)
			{
				printf("%c%c", '6', '6');
			}
			else
			{
				printf("%c", '6');
			}
		}
	}
	printf("\n");
	
	return 0;
}

二、Python解答

str = input()
i = 0
count = 0
while i < len(str):
    if str[i] != '6':
        print(str[i], end="")
        i += 1
    else:
        count += 1
        i += 1
        while i < len(str) and str[i] == '6':
            count += 1
            i += 1
        if count > 9:
            print('27', end='')
        elif count > 3:
            print('9', end='')
        elif count == 3:
            print("666", end='')
        elif count == 2:
            print("66", end='')
        elif count == 1:
            print("6", end='')
        count = 0

总结

提示:无。

举报

相关推荐

0 条评论