0
点赞
收藏
分享

微信扫一扫

蓝桥杯-第十届蓝桥杯 C语言A组/B组/C组/研究生组-Python题解

幸福的无所谓 2022-05-03 阅读 81

#判断数字中是否含有2,0,1,9
def has2019(n):
    flag=False
    while n!=0:
        a=n%10
        if a==2 or a==0 or a==1 or a==9:
            flag=True
            return flag
        #注意python的除法机制
        n//=10
    return flag

sum=0
for i in range(1,2019+1):
    if has2019(i):
        sum+=i**2

print(sum)

#利用动态数组
#不必申请过多空间
item_list=[0 for i in range(4)]
item_list[1]=1
item_list[2]=1
item_list[3]=1
#计算每一项的值
#因为每一项只和其前3项有关
#计算之后需要更新列表元素值
for i in range(4,20190324+1):
    temp=sum(item_list[1:4])
    #只需最后4位数
    temp%=10000
    #更新元素值
    item_list[1]=item_list[2]
    item_list[2]=item_list[3]
    item_list[3]=temp

print(item_list[3])

 

 

#读取迷宫地图
file=open('E:\project_code\蓝桥杯\maze.txt','r')
read_map=file.readlines()
#迷宫中坐标和值的映射关系
point_value_map={}

#建立迷宫
#先在迷宫外面建墙,方便搜索迷宫
for i in range(0,32):
    point_value_map[(i,0)]='1'
    point_value_map[(i,51)]='1'
for i in range(0,52):
    point_value_map[(0,i)]='1'
    point_value_map[(31,i)]='1'
x=1
for line in read_map:
    #读取到的迷宫每一行最后一个是\n换行符
    for i in range(len(line)-1):
        point_value_map[(x,i+1)]=line[i]
    x+=1

#对迷宫按照题目中规定的字典序进行广度优先搜索
from collections import deque
bread_first_queue=deque()
#当前节点是否已经遍历过
has_checked=[]
#遍历的起始点和终止点的坐标
start_point=(1,1)
end_point=(30,50)
bread_first_queue.append(start_point)
while bread_first_queue:
    now_point=bread_first_queue.popleft()
    #如果当前节点没有遍历
    if now_point not in has_checked:
        #如果当前节点是终点,则广度优先搜索结束
        if now_point==end_point:
            break
        else:
            #按照题目规定对四个方向进行搜索
            #D<L<R<U
            down_point=(now_point[0]+1,now_point[1])
            left_point=(now_point[0],now_point[1]-1)
            right_point=(now_point[0],now_point[1]+1)
            up_point=(now_point[0]-1,now_point[1])
            #标记当前节点已经被访问过
            has_checked.append(now_point)

            if point_value_map.get(down_point)=='0' and (down_point not in has_checked):
                bread_first_queue.append(down_point)
            if point_value_map.get(left_point)=='0' and (left_point not in has_checked):
                bread_first_queue.append(left_point)
            if point_value_map.get(right_point)=='0' and (right_point not in has_checked):
                bread_first_queue.append(right_point)
            if point_value_map.get(up_point)=='0' and (up_point not in has_checked):
                bread_first_queue.append(up_point)
#从所有的可能路径中寻找字典序最小路径
total_road=has_checked+[end_point]
#最小路径坐标序列
sequence_of_point=[]
#最小路径的字母表示
sequence_of_direction=[]
#当前坐标点,从终点向起点回溯
now_point=(end_point)
while now_point != start_point:
    down_point = (now_point[0] + 1, now_point[1])
    left_point = (now_point[0], now_point[1] - 1)
    right_point = (now_point[0], now_point[1] + 1)
    up_point = (now_point[0] - 1, now_point[1])
    if point_value_map.get(up_point)=='0' and up_point in total_road:
        sequence_of_point.append(up_point)
        index=total_road.index(up_point)
        #删除掉其它路径
        total_road=total_road[:index]
        sequence_of_direction.append('D')
        now_point=up_point
    elif point_value_map.get(right_point)=='0' and right_point in total_road:
        sequence_of_point.append(right_point)
        index=total_road.index(right_point)
        #删除掉其它路径
        total_road=total_road[:index]
        sequence_of_direction.append('L')
        now_point=right_point
    elif point_value_map.get(left_point)=='0' and left_point in total_road:
        sequence_of_point.append(left_point)
        index=total_road.index(left_point)
        #删除掉其它路径
        total_road=total_road[:index]
        sequence_of_direction.append('R')
        now_point=left_point
    elif point_value_map.get(down_point)=='0' and down_point in total_road:
        sequence_of_point.append(down_point)
        index=total_road.index(down_point)
        #删除掉其它路径
        total_road=total_road[:index]
        sequence_of_direction.append('U')
        now_point=down_point

#结果字符串
result_string=''
for i in sequence_of_direction:
    result_string+=i
print(result_string[::-1])

n=int(input())
tree_list=list(map(int,input().split()))
from math import log
#求树深
tree_depth=int(log(n,2))+1
#最大节点权值和
max_value=0
#最大节点权值和所在的层次
max_depth=0
for i in range(tree_depth):
    temp=sum(tree_list[2**i-1:2**(i+1)-1])
    if temp>max_value:
        max_value=temp
        max_depth=i+1

print(max_depth)

#模拟题,按照题目中的规则进行模拟
n,m,t=map(int,input().split())
#商家列表
shop_list=[0 for i in range(n+1)]
#订单信息列表
order_list=[]
for i in range(m):
    order_time,order_id=map(int,input().split())
    order_list.append([order_time,order_id])
#将订单按照时间顺序进行排序
order_list.sort(key=lambda x:x[0])
#优先缓存队列
catch=[]

#按照时间顺序进行商家优先级更新
for time in range(1,t+1):
    #假设time时刻所有商家都没有订单
    flag=True
    #遍历订单列表,寻找当前时刻订单信息
    for order_item in order_list:
        if order_item[0]==time:
            #当前时刻存在订单
            flag=False
            #更新相应商家优先级
            shop_list[order_item[1]]+=2
            #更新其它商家优先级
            for i in range(1,n+1):
                if i==order_item[1]:
                    continue
                else:
                    if shop_list[i]==0:
                        continue
                    else:
                        shop_list[i]-=1
    #当前时刻所有商家都没有订单
    if flag:
        for i in range(1,n+1):
            if shop_list[i]==0:
                continue
            else:
                shop_list[i]-=1
    #根据商家优先级更新缓存队列
    for i in range(1,n+1):
        if shop_list[i]>5 and (i not in catch):
            catch.append(i)
        if shop_list[i]<=3 and (i in catch):
            catch.remove(i)

print(len(catch))

n=int(input())
number_list=list(map(int,input().split()))
for i in range(len(number_list)):
    while number_list[i] in number_list[:i]:
        number_list[i]+=1

print(number_list)

 

 

 

# 深度优先搜索,寻找最优组合
# 每名队员只能担任一个位置

# 最大评分
max_value = -1
# 编号,评分表
id_score_table = [[0] * 7 for i in range(21)]

# 标记当前队员是否已经访问
has_checked = []

# 读取文件
file = open('E:/project_code/蓝桥杯/team.txt', 'r')
read_table = file.readlines()
# 建立表格,从(1,1)开始
x = 1
for item in read_table:
    #读取到的内容是以字符串形式返回
    #需要处理一下
    
    #将最后一个\n符号去掉
    item=item[:-1]
    #从字符串分割出数字
    item=item.split()
    for i in range(len(item)):
        #这里字符串需要转换为数字
        id_score_table[x][i+1] = int(item[i])
    x+=1
    
#深度优先搜索
def dfs(temp_max,depth):
    global max_value
    #temp_max是当前组合最大值
    #depth是当前在第几列
    
    #已经遍历完所有的列
    if depth==7:
        max_value=max(temp_max,max_value)
        return
    
    #遍历每个队员的组合
    for i in range(1,21):
        #如果当前队员还没有被遍历
        if i not in has_checked:
            has_checked.append(i)
            #向下一列继续试探
            dfs(temp_max+id_score_table[i][depth],depth+1)
            #试探结束,回溯
            has_checked.remove(i)
            
#寻找最大值
for i in range(1,21):
    has_checked.append(i)
    dfs(id_score_table[i][2],3)
    has_checked.remove(i)

print(max_value)

x=2019
ans=''
while x!=0:
    #A的ASCII码为65
    #从64计算
    ans+=chr(x%26+64)
    x//=26

print(ans[::-1])

#判断数字中是否含有2,4
def has24(n):
    #假设不含有
    flag=False
    while n!=0:
        a=n%10
        if a==2 or a==4:
            flag=True
            return flag
        n//=10
    return flag
count=0

#按照i<j<k进行试探组合
for i in range(1,2019+1):
    if has24(i):
        continue
    for j in range(i+1,2019+1):
        if has24(j):
            continue
        k=2019-i-j
        #按照i<j<k顺序进行试探
        if k<=i or k<=j or has24(k):
            continue
        else:
            count+=1

print(count)

 

n=int(input())
number_list=list(map(int,input().split()))
#将数列排序,求出等差数列的公差
number_list.sort()
#公差就是相邻元素的最小差值
min_value=10**10
for i in range(len(number_list)-1):
    temp=number_list[i+1]-number_list[i]
    if temp<min_value:
        min_value=temp
#计算等差数列的项数
ans=0
for i in range(number_list[0],number_list[len(number_list)-1]+1,min_value):
    ans+=1

print(ans)

#按照题目要求进行模拟
a=2019
b=324
ans=0
#切割最后可能有两种情况
#1.有一个边长为0,完全切割,直接退出即可
#2.有一个边长为1,切割数加上最后的最大边长即可

#标记边长是否为1
flag=False
while True:
    temp=a
    #进行一次切割
    #从大的边长中减去小的边长
    a=max(temp-b,b)
    b=min(temp-b,b)
    ans+=1
    if a==1 or b==1:
        flag=True
        break
    if a==0 or b==0:
        break
if flag:
    ans+=max(a,b)

print(ans)

#质数列表
prime_number_list=[]
#素数筛法数表
number_list=[i for i in range(2,100001)]
#素数筛法
for i in range(len(number_list)):
    if number_list[i]!=0 and number_list[i] not in prime_number_list:
        #添加当前素数
        prime_number_list.append(number_list[i])
        if len(prime_number_list)>=2019:
            break
        for j in range(i+1,len(number_list)):
            if number_list[j]%number_list[i]==0:
                number_list[j]=0

print(prime_number_list.pop(2018))

 

 

#顺时针旋转90
n,m=map(int,input().split())
matrix=[[0]*m for i in range(n)]
for i in range(n):
    matrix[i]=list(map(int,input().split()))
#先将矩阵倒序,然后行列互换
matrix[::] = [[row[i] for row in matrix[::-1]] for i in range(len(matrix[0]))]

print(matrix)

 

 

 

 

#字符串处理问题
k=int(input())
string=input()
#正则匹配模块
import re
#将字符串按照 '.' 和空格进分割
#'.+空格'将被分解成一个空字符
string_list=re.split('\s+|\.',string)
#总的出现次数为:
#alice->bob的距离+bob->alice的距离
ans=0
for i in range(len(string_list)):
    if string_list[i]=='Alice':
        for j in range(i+1,len(string_list)):
            #当前字符串后面有一个空格
            #分割的时候被删掉了
            #所以sum应该从1开始计数
            sum=1
            if string_list[j]=='Bob':
                #计算中间出现的字符数
                for k in range(i+1,j):
                    #同样因为空格要+1
                    sum+=(len(string_list[k])+1)
        if sum<=k:
            ans+=1
            #只计算一次最短距离
            break
#bob->alice的距离
for i in range(len(string_list)):
    if string_list[i]=='Bob':
        for j in range(i+1,len(string_list)):
            #当前字符串后面有一个空格
            #分割的时候被删掉了
            #所以sum应该从1开始计数
            sum=1
            if string_list[j]=='Alice':
                #计算中间出现的字符数
                for k in range(i+1,j):
                    #同样因为空格要+1
                    sum+=(len(string_list[k])+1)
        if sum<=k:
            ans+=1
            #只计算一次最短距离
            break

print(ans)

string=input()
ans=0
#26进制转为10进制
for i in range(len(string)):
    #字符i的ascii码
    number=ord(string[i])
    ans+=(number-64)*(26**(len(string)-(i+1)))

print(ans)

 

 

 

按照以下过程进行计算:

n=p*q

p,q为素数

d与(p-1)*(q-1)互素

(d*e)%((p-1)*(q-1))=1

c=x^d%n

x=c^e%n

给定d,c,n

求x

#利用素数筛法找出p,q
p=891234941
q=1123984201
n=1001733993063167141
c=20190324
d=212353
for i in range(1,500000):
    if(((p-1)*(q-1)*i+1)%d==0):
        e=((p-1)*(q-1)*i+1)//212353
        print(((p-1)*(q-1)*i+1)//d)
        break
def quick_mod(a,b,c):
    a=a%c
    ans=1
    while b!=0:
        if b&1:
            ans=(ans*a)%c
        b>>=1
        a=(a*a)%c
    return ans
x=quick_mod(c,e,n)
print(x)
print(quick_mod(x,d,n))

计算斐波那契数列的第N项/第N+1项的值

number_list=[0 for i in range(3)]
number_list[1]=1
number_list[2]=1
n=int(input())
for i in range(3,n+1+1):
    temp=number_list[1]+number_list[2]
    number_list[1]=number_list[2]
    number_list[2]=temp

print('%.8f'%(number_list[1]/number_list[2]))

 

 

#迪杰斯特拉求最短路径
def shortPath(start_point,graph):
    #start_point是求最短路径的起点
    #graph是领接矩阵
    #已经求出最短路径的顶点
    has_past=[]
    #还没有求出最短路径的顶点
    no_past=[]
    #从start_point到其它顶点的最短距离
    dis=[]
    #初始化
    #从起点开始算到其它顶点的最短路径
    has_past.append(start_point)
    #除了起点其它没有求出最短路径的顶点
    no_past=[x for x in range(len(graph)) if x != start_point]
    #最初的从起点到其它顶点的最短距离默认为领接矩阵顶点所在的行
    dis=graph[start_point]

    #当还有没计算出最短距离的顶点时
    while len(no_past):
        id=no_past[0]
        #寻找从起点到其它顶点的最短距离
        for i in no_past:
            if dis[i]<dis[id]:
                id=i
        #借助第id个顶点松弛起点到其它顶点的最短距离
        for i in no_past:
            if dis[id]+graph[id][i]<dis[i]:
                dis[i]=dis[id]+graph[id][i]
    return dis


举报

相关推荐

0 条评论