蓝桥杯训练13——奇怪的数列

蓝桥杯训练13——奇怪的数列_第1张图片

def teshu(m):
    new_m = ""  # 定义一个空字符串
    count = 1
    first_m = m[0]  # 初始的m[0]为first_m
    # 从m的第二个参数开始遍历m[1]
    for i in m[1:]:
        if i == first_m:
            count += 1  # 统计m[]出现的次数
        else:
            new_m += str(count) + first_m  # 将次数和m[]连一起
            # m[0]不等于i时重置一下count
            count = 1
            # first_m由m[0]变i
            first_m = i
    new_m += str(count) + first_m  # 最后一段相同的字符
    return new_m

# 读取输入
m = input()
n = int(input())

# 循环调用 teshu 函数
for _ in range(n):
    m = teshu(m)

print(m)

你可能感兴趣的:(蓝桥杯,职场和发展)