蓝桥杯刷题(十)

1.翻转

代码

输入数据,每组数据进行比较,j的范围掐头去尾,若aj==bj,继续,若出现010,101子串则改成000,111,遍历完后比较a是否等于b,相同则输出次数,不同则输出-1。

python 复制代码
for _ in range(int(input())):
    a = list(input())
    b = list(input())
    cnt = 0
    for j in range(1,len(a)-1):
        if a[j] == b[j]:
            continue
        elif b[j-1]==b[j+1] and b[j] != b[j-1]:
            b[j]=b[j-1]
            cnt += 1
    print(cnt if a==b else -1)

2.取模

暴力:(只能通过90%)

python 复制代码
def f(n,m)->bool:
    for y in range(1,m+1):
        for x in range(1,y):
            if n%x == n%y:
                return True
    return  False

t = int(input())
for _ in range(t):
    a,b = map(int,input().split())
    print('Yes' if f(a,b) else 'No')

抽屉原理:

python 复制代码
for _ in range(int(input())):
  chk=0
  n,m=map(int,input().split())
  for i in range(m,1,-1):
    if(n%i != (i-1)):
      chk=1
      break
  print("Yes") if chk else print("No")
相关推荐
JieE2126 小时前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2126 小时前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
hboot7 小时前
AI工程师第二课 - 数据处理
人工智能·python·数据分析
vivo互联网技术11 小时前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
用户83562907805112 小时前
使用 Python 自动化 PowerPoint 形状布局与格式设置
后端·python
Darling噜啦啦12 小时前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
用户83562907805113 小时前
用 Python 自动化 PowerPoint 演讲者备注添加
后端·python
用户4978630507315 小时前
(一)小红的数组操作
算法·编程语言
怕浪猫18 小时前
Electron 系列文章封面图
算法·架构·前端框架