【一眼秒杀】三道蓝桥杯真题(五)

一、压缩字符串

s = input()
compressed = ""
count = 1

for i in range(len(s)):
    # 如果当前字符与下一个字符相同,则增加计数
    if i < len(s) - 1 and s[i] == s[i + 1]:
        count += 1
    else:
        # 否则,将字符和计数添加到压缩字符串中
        if count > 1:
            compressed += s[i] + str(count)
        else:
            compressed += s[i]
        count = 1  # 重置计数
if len(compressed) < len(s):  
    print(compressed)
else:
    print("NO")

二、Fizz Buzz 经典问题

N = int(input())
if N % 3 == 0:
    if N % 5 == 0:
        print('FizzBuzz')
    else:
        print('Fizz')
elif N % 5 == 0:
    print('Buzz')
else:
    print(N)

三、拼数

n=int(input())
m=input().split()
#输入

for i in range(n-1):
  for j in range(i+1,n):
    if m[i]+m[j]<m[j]+m[i]:
      m[i],m[j]=m[j],m[i]
#交换排序,节省时间,全排可能超时

print(''.join(m))
#一个输出方法

每天持续更新~

上一篇文章: 【一眼秒杀】三道蓝桥杯真题(四)-CSDN博客

猜你喜欢

转载自blog.csdn.net/weixin_57467129/article/details/140558031