本届比赛:两道填空,八道编程题 一共150分
题解都是个人思路,不代表全部正确,也有可能出现超时的情况,欢迎大家指正。
超时也不用太担心,因为蓝桥杯是OI赛制,每通过一个测试点,就会一定的分值
试题A 排列字母
s = 'WHERETHEREISAWILLTHEREISAWAY' print(''.join(sorted(s))) # 对于字符串可以按字典序进行排序 # AAAEEEEEEHHHIIILLRRRSSTTWWWY
试题B 寻找整数
分析题目
因为能被11和17整除,11和17互质,所以该整数为187的倍数,
解题分为两步,找出满足一部分数的步长,因为数量级较大,所以找到的步长尽量大
答案:2022040920220409
s = 187 c = 0 # 该整数是187的倍数,且不能被2整除,既为奇数 for i in range(187, 10 ** 17, 374): # 开始为187,既步长为374 if i % 49 == 46 and i % 48 == 41 and i % 47 == 5 and i % 46 == 15 and i % 45 == 29: # 因为需要哦的步长需要很大,所以选数量较大的数 c += 1 print(i) if c > 5: break print(12590206409 - 5458460249) # 7131746160 print(19721952569 - 12590206409) # 7131746160 发现规律,开始满足条件的数是5458460249,以后的间隔是7131746160的倍数 mod = [(2, 1), (3, 2), (4, 1), (5, 4), (6, 5), (7, 4), (8, 1), (9, 2), (10, 9), (11, 0), (12, 5), (13, 10), (14, 11), (15, 14), (16, 9), (17, 0), (18, 11), (19, 18), (20, 9), (21, 11), (22, 11), (23, 15), (24, 17), (25, 9), (26, 23), (27, 20), (28, 25), (29, 16), (30, 29), (31, 27), (32, 25), (33, 11), (34, 17), (35, 4), (36, 29), (37, 22), (38, 37), (39, 23), (40, 9), (41, 1), (42, 11), (43, 11), (44, 33), (45, 29), (46, 15), (47, 5), (48, 41),(49,46) ] for i in tqdm(range(5458460249, 10 ** 17, 7131746160)): # 开始位置是5458460249 步长为7131746160 for a, b in mod: if i % a != b: break else: print(i) # for else结构,当for正常执行结束,则运行else语句 break
试题C 纸张尺寸
l = 1189 w = 841 name = input() num = int(name[1:]) for i in range(num): t = l//2 # 整除 l = w w = t print(l) print(w)
试题D 位数排序
使用字典表达式和字典排序
源代码精简
n = int(input()) m = int(input()) # 字典解析 dic = {k:sum([int(i) for i in str(k)])for k in range(1,n+1)} # 根据字典的值进行排序 print(sorted(dic.items(),key=lambda item:item[1])[m-1][0])
详细代码
n = int(input()) m = int(input()) dic= {k:0 for k in range(1,n+1)} # 创建字典 for num,s in dic.items(): st_list = list(str(num)) # 将数的每一位都分割开来 st_list = [int(i) for i in st_list] # 数据类型转换 dic[num] = sum(st_list) # 求和 dic_lst = sorted(dic.items(), key=lambda item: item[1]) # 根据值进行排序,列表,元素为元组 m = dic_lst[m-1][0] print(m)
试题E 蜂巢
暂时还没有好的思路