本届比赛:两道填空,八道编程题 一共150分
题解都是个人思路,不代表全部正确,也有可能出现超时的情况,欢迎大家指正。
超时也不用太担心,因为蓝桥杯是OI赛制,每通过一个测试点,就会一定的分值
试题A 排列字母
s = 'WHERETHEREISAWILLTHEREISAWAY'
print(''.join(sorted(s)))
试题B 寻找整数
分析题目 因为能被11和17整除,11和17互质,所以该整数为187的倍数, 解题分为两步,找出满足一部分数的步长,因为数量级较大,所以找到的步长尽量大 答案:2022040920220409
s = 187
c = 0
for i in range(187, 10 ** 17, 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)
print(19721952569 - 12590206409)
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)):
for a, b in mod:
if i % a != b:
break
else:
print(i)
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 蜂巢
暂时还没有好的思路
试题F 消除游戏
st = input()
st_list = list(st)
st_flag = [0 for _ in range(len(st_list))]
for c in range(2**64):
st_flag = [0 for _ in range(len(st_list))]
l1 = len(st_list)
for i in range(1,len(st_list)-1):
if st_list[i]==st_list[i-1] and st_list[i]!= st_list[i+1]:
st_flag[i] = 1
st_flag[i+1] = 1
if st_list[i]!=st_list[i-1] and st_list[i]== st_list[i+1]:
st_flag[i] = 1
st_flag[i-1] = 1
st_list2 = []
for i in range(len(st_flag)):
if st_flag[i] == 0:
st_list2.append(st_list[i])
st_list = st_list2
l2 = len(st_list)
if l2 == l1:
break
st = ''.join(st_list)
if len(st) == 0:
print('EMPTY')
else:
print(st)
试题G 全排列的价值
利用回溯法求解全排列 关于这个算法的详细情况我回来会单独写一篇文章讲解
n = int(input())
arr = [i for i in range(1, n + 1)]
def permutations(arr, position, end):
"""
生成所有的全排列情况,将所有的情况存入另一个数组中
:param arr: 数组
:param position: 开始交换的位置
:param end: 结束的位置
:return:
"""
if position == end:
arr_s.append(arr.copy())
else:
for index in range(position, end):
arr[index], arr[position] = arr[position], arr[index]
permutations(arr, position + 1, end)
arr[index], arr[position] = arr[position], arr[index]
permutations(arr, 0, len(arr))
def list_value(lst):
"""
判断每一个数组里面的价值
:param lst: 全排列的一种情况
:return: 价值
"""
value = 0
for i in range(1, len(lst)):
for j in range(i):
if lst[i] > lst[j]:
value += 1
return value
sum_value = 0
for item in arr_s:
sum_value += list_value(item)
print(sum_value)
使用内置函数求解
from itertools import permutations
n = int(input())
arr = [i for i in range(1, n + 1)]
arr_s = list(permutations(arr))
def list_value(lst):
"""
判断每一个数组里面的价值
:param lst: 全排列的一种情况
:return: 价值
"""
value = 0
for i in range(1, len(lst)):
for j in range(i):
if lst[i] > lst[j]:
value += 1
return value
sum_value = 0
for item in arr_s:
sum_value += list_value(item)
print(sum_value)
试题H 技能升级
import math
n,m = map(int,input().split())
a_lst = []
b_lst = []
c_lst = []
for i in range(n):
a,b = map(int,input().split())
a_lst.append(a)
b_lst.append(b)
c = math.ceil(a/b)
c_lst.append(c)
s = 0
for i in range(m):
max_num = max(a_lst)
index = a_lst.index(max_num )
if c_lst[index]>0:
s+=max_num
a_lst[index] = max_num-b_lst[index]
c_lst[index] -= 1
print(s)
试题I 最长不下降子序列
题意理解 其实就是求解间隔k的长度为最长不递减序列
求最长的L1和L2
n,k = map(int,input().split())
nums = [int(k) for k in input().split()]
def funs(lst):
"""
lst[0:a] 是一个不递减的列表
:param lst: 原列表
:return: lst[0:a]的长度
"""
a = 1
for i in range(len(lst) - 1):
if lst[i] >= lst[i + 1]:
break
a += 1
return a
left = 0
max_l = 1
while True:
lst1 = nums[left:]
l1 = funs(lst1)
if l1+left+k>=n:
if n-left> max_l:
max_l = n-left
break
if nums[l1+left] <= nums[l1+left+k]:
lst2 = nums[l1+left+k:]
l2 = funs(nums[l1+k:])
if l2+l1+k > max_l:
max_l = l2+l1+k
else:
if l1+k>max_l:
max_l = l1+k
left+=1
print(max_l)
最优清零方案
n,k = map(int,input().split())
nums = [int(i) for i in input().split()]
left = 0
c = 0
while left <= n-k:
lst = nums[left:left+k]
if 0 in lst:
left+=1
continue
min_num = min(lst)
for i in range(len(lst)):
lst[i] = lst[i] - min_num
nums[left:left+k] = lst
c+=min_num
left += 1
print(c+sum(nums))
最后说一下我个人感觉获奖的大概情况: 省一:1道填空,4道大题 省二:1道填空,2道大题+部分试题通过部分 省三:1道填空,1道大题 这些是根据我和我周围同学的总结出来的,仅供参考,得奖与否还要看当年题目的难易程度
既然都看到这里了,点个赞、点个关注再走吧
|