Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)

Python算法学习: 计蒜客蓝桥杯训练营练习

(因为本人也是菜鸟)不是所有的代码都百分百通过,一些超时没有解决,很多也没有写出来,希望大佬指出

由于网上很多没有python解题的,所以分享一下

Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第1张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第2张图片

import random
n = int(input())
list = []
for i in range(n):
    a = random.randint(1,1001)
    list.append(a)
for i in range(0,len(list)):
    for j in range(i+1,len(list)):
        if list[i] == list[j]:
            list.remove(list[i])
list.sort()
print(list)

在这里插入图片描述Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第3张图片

n,r = map(int,input().split())
if r>=10 and r<=16:
        a = int(n/r)
        b = n%r
        if b == 10:
            print("%dA"%a)
        if b == 11:
            print("%dB"%a)
        if b == 12:
            print("%dC"%a)
        if b == 13:
            print("%dD"%a)
        if b == 14:
            print("%dE"%a)
        if b == 15:
            print("%dF"%a)
if r>=0 and r<=10:
    g = []
    a = 2
    while a>0:
        # print(r,n,a)
        a = n%r
        # print(r, n, a)
        n = int(n/r)
        g.append(a)
    # g.append(a%r)
    g.reverse()
    if len(g)>1:
        g.remove(0)
    for i in g:
        print(i,end='')

回文数

Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第4张图片
Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第5张图片

n = input()
def huiwen(n):
    list = []
    list.append(n)
    y = ''
    m = 0
    for i in n:
        y = i + y
    if n!=y:
        y=''
    while n!=y:
        list.append('--->')
        m=m+1
        y = ''
        for i in n:
            y = i + y
        n = int(n)
        y = int(y)
        n = n+y
        list.append(n)
        n = str(n)
        y = ''
        for i in n:
            y = i + y
    list.append(m)
    return list
a = huiwen(n)
print(a[-1])
a.remove(a[-1])
for i in a:
    print(i,end='')

机器人

Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第6张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第7张图片

n = int(input())
dx = [0,-1, 0, 1]  #上左下右
dy = [1, 0,-1, 0]  #上左下右
now_dir = 3
nowx = 0
nowy = 0
for i in range(0,n):
    dir,x = map(str,input().split())
    x = int(x)
    if dir[0] == 'b':
        now_dir = (now_dir+2)%4
    elif dir[0] == 'l':
        now_dir = (now_dir+1)%4
    elif dir[0] == 'r':
        now_dir = (now_dir+3)%4
    nowx += dx[now_dir]*x
    nowy += dy[now_dir]*x
print(nowx,nowy)

字符串

在这里插入图片描述Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第8张图片

n = input()
x = ''
num_ascii = 0
for i in n:
    num_ascii =ord(i)
    if num_ascii>57:
        num_ascii+=1
        if num_ascii == 91:
            num_ascii = 65
        elif num_ascii == 123:
            num_ascii = 97
        y = chr(num_ascii)
        x = x+y
    else :
        x = x+chr(num_ascii)
print(x)

红绿蓝

在这里插入图片描述Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第9张图片

n = input()
list = []
num = 0
for i in n:
    list.append(i)
r = list.count('R')
g = list.count('G')
b = list.count('B')
print('B'*b,'G'*g,'R'*r,sep='')
g = int(g/2)
b = int(b/3)
num = min(r,g,b)
print(num)

递归函数

Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第10张图片

def f(x):
    if x<=0:
        return 0
    if x==1:
        return 1
    if x>1 and x%2==0:
        return 3*f(int(x/2))-1
    if x>1 and x%2==1:
        return 3*f(int((x+1)/2))-1
n = int(input())
x = f(n)
print(x)

抢气球

Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第11张图片
Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第12张图片

n,m = map(int,input().split())
x = input().split()
y = input().split()
child_high = []
ball_high =[]
for i in x:
    i = int(i)
    child_high.append(i)
for i in y:
    i = int(i)
    ball_high.append(i)
child = {
     }
child1 = {
     }
for i in range(n):
    child[i+1] = child_high[i]
for i in range(n):
    child1[child_high[i]] = i+1
child_num = []
for i in child.values():
    child_num.append(i)
child_num.sort()
for i in child_num:
    ican=0
    x = 0
    for j in range(len(ball_high)):
        if i>=ball_high[x]:
            ican+=1
            ball_high.remove(ball_high[x])
        else:x+=1
    child[child1[i]]=ican
for i in child.keys():
    print(child[i])

报数

在这里插入图片描述Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第13张图片

n,m = map(int,input().split())
list = [i for i in range(1,n+1)]
baoshu =0
while len(list)>1:
    baoshu+=1
    list.append(list.pop(0))
    if baoshu==m:
        baoshu =0
        list.pop()
print(list[0])

敲7

Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第14张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第15张图片

x,y,z = map(int,input().split())
name_list =[]
for i in range(x):
    name = input()
    name_list.append(name)
for i in range(y-1):
    name_list.append(name_list.pop(0))
while len(name_list)>1:
    z+=1
    name_list.append(name_list.pop(0))
    if z%7==0 or '7' in str(name_list[-1]):
        name_list.pop()    
print(name_list[0])

装饰效果

Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第16张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第17张图片

n = int(input())
list = list(input().split())
xiaoguo = []
for i in list:
    i=int(i)
    xiaoguo.append(i)   
max = 0
for i in range(n):
    tmp = 0
    for j in range(i,n):
        tmp = tmp+xiaoguo[j]
        if tmp>max:
            max = tmp
print(max)

贪心算法:

奶酪工厂

Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第18张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第19张图片

n,m =map(int,input().split())
zhou =[]
jiaofu =[]
now = 99999
sum =0
for i in range(n):
    n =input().split()
    x = int(n[0])
    y = int(n[1])
    now = min(now+m,x)
    sum = sum+now*y
print(sum)

蘑菇森林

Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第20张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第21张图片

n,m,h,b = map(int,input().split())
mingzhong = h+b
sum = 0
master_list = []
for i in range(n):
    x,y=map(int,input().split())
    if mingzhong>=x:
        master_list.append(y)
master_list.sort()
for i in master_list:
    if m>=i:
        sum+=1
        m = m-i
print(sum)

二分法——开花

在这里插入图片描述Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第22张图片

def erfen(list,num):
    left = 0
    right = len(list)-1
    while left<=right:
        mid = (right+left)//2
        if num==list[mid]:
            return num
        elif num>list[mid]:
            left = mid+1
        elif num<list[mid]:
            right = mid-1
    else :
        return -1
n,m = map(int,input().split())
x = input().split()
y = input().split()
wen =[]
ti =[]
for i in x:
    i = int(i)
    wen.append(i)
for i in y:
    i = int(i)
    ti.append(i)
ti.sort()
for i in wen:
    x = erfen(ti,i)
    if x>0:
        print(x,end=' ')

切割钢管

Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第23张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第24张图片

n,k = map(int,input().split())
l = input().split()
gang = []
for i in l:
    i = int(i)
    gang.append(i)
left = 0
right = 1000000001
while left<=right:
    mid = (right+left)//2
    num = 0
    for i in gang:
        num+=i//mid
    if num>=k:
        left =mid+1
    elif num<k:
        right =mid-1
print(right)

跳石头

Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第25张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第26张图片

l,n,m = map(int,input().split())
stone = []
for i in range(n):
    x = int(input())
    stone.append(x)
def check(x):
    cnt= 0
    now=0
    for i in range(n):
        if stone[i]-stone[now]<x:
            cnt+=1
        else:
            now=i
    if cnt>m:
        return False
    else:
        return True
def binarySearch(left,right):
    ans =0
    while left<=right:
        mid = (left+right)//2
        x = check(mid)
        if x==True:
            ans= mid
            left=mid+1
        else:
            right=mid-1
    return ans
stone.append(l)
print(binarySearch(0,l))

笨小猴

Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第27张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第28张图片

n =input()
maxn =0
minn =99999
for i in n:
    num = n.count(i)
    if num>maxn:
        maxn=num
    if num<minn:
        minn=num
def check(x):
    if x>1:
        for i in range(2,x):
            if i%x==0:
                return -1
        else:
            return 1
    else:
        return -1
x = check(maxn-minn)
if x>0:
    print('Lucky Word')
    print(maxn-minn)
else:
    print('No Answer')
    print(0)

蒜头君面试
Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第29张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第30张图片

from collections import Counter
n = input()
x = input().split()
list =[]
for i in x:
    i = int(i)
    list.append(i)
count = Counter(list).most_common()#返回一个列表,列表每个元素出现的次数是元组
top = Counter(list).most_common(1)#取出次数最多的 返回值是一个列表套元组[(i,次数)]
ans = -9999999999
for i in range(len(count)):
    tmp =-9999999999
    if count[i][1]==top[0][1]:
        tmp=count[i][0]
    if tmp>ans:
        ans=tmp
print(ans,top[0][1])

N个最小和
在这里插入图片描述Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第31张图片

import heapq
n = int(input())
a = input().split()
b = input().split()
A = []
B = []
num =[]
heapq.heapify(num)
for i in a:
    i = int(i)
    A.append(i)
for i in b:
    i = int(i)
    B.append(i)
for i in A:
    for j in B:
        tmp = 0
        tmp = i+j
        heapq.heappush(num,tmp)
for i in range(n):
    x = heapq.heappop(num)
    if i<n-1:
        print(x,end=' ')
    else:
        print(x,end='')

银行的客户队列
Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第32张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第33张图片

import heapq
bank = []
conter = {
     }
heapq.heapify(bank)
while True:
    n = input().split()
    if n[0] =='0':
        break
    elif n[0] == '1':
        conter[int(n[2])] =int(n[1])
        heapq.heappush(bank,int(n[2]))
    elif n[0] =='2':
        if len(bank)==0:
            print(0)
        else:
            x = heapq.nlargest(1,bank)
            bank.remove(x[0])
            print(conter[x[0]])
    elif n[0] =='3':
        if len(bank) == 0:
            print(0)
        else:
            x = heapq.nsmallest(1,bank)
            bank.remove(x[0])
            print(conter[x[0]])

中国象棋
Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第34张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第35张图片

list = []
for i in range(10):
    x = input()
    list.append(x)
dir=[[2,1],[2,-1],[-2,1],[-2,-1],[1,2],[-1,2],[1,-2],[-1,-2]]
his_list =[]
def dfs(x,y):
    if list[x][y]=='T':
        return True
    for i in range(8):
        his_list.append([x,y])
        tx = x+dir[i][0]
        ty = y+dir[i][1]
        if ([tx,ty] not in his_list and tx>=0 and tx<=9 and ty>=0 and ty<=8):
            if list[tx][ty]!='#':
                if dfs(tx,ty):
                    return True

    return False

if __name__=='__main__':
    for i in range(10):
        for j in range(9):
            if list[i][j]=="S":
                sx = i
                sy = j

    if dfs(sx,sy):
        print('Yes')
    else:
        print('No')

踏青
Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第36张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第37张图片

dir = [[1,0],[-1,0],[0,1],[0,-1]]
his_list =[]
def dfs(x,y):
    for i in range(4):
        his_list.append([x,y])
        tx = x+dir[i][1]
        ty = y+dir[i][0]
        if ([tx,ty] not in his_list and tx>=0 and tx<n and ty>=0 and ty<m):
            if maze[tx][ty]=='#':
                dfs(tx,ty)

if __name__=="__main__":
    n, m = map(int, input().split())
    maze = []
    ans =0
    for i in range(n):
        x = input()
        maze.append(x)

    for i in range(n):
        for j in range(m):
            if maze[i][j]=='#' and [i,j] not in his_list:
                dfs(i,j)
                ans+=1
    print(ans)

引爆炸弹
Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第38张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第39张图片

def dfs(x,y):
    maze[x][y]=0
    for i in range(n):
        if maze[i][y]==1:
            dfs(i,y)
    for i in range(m):
        if maze[x][i]==1:
            dfs(x,i)
n,m =map(int,input().split())
ans =0
maze = []
for i in range(n):
    a = []
    x = input()
    for i in x:
        i = int(i)
        a.append(i)
    maze.append(a)

for i in range(n):
    for j in range(m):
        if maze[i][j]==1:
            ans+=1
            dfs(i,j)
print(ans)

超级书架
Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第40张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第41张图片

n,m = map(int,input().split())
cow_high =[]
for i in range(n):
    x = int(input())
    cow_high.append(x)
list =[]
def dfs(u,sum):
    if u==n or sum>=m:
        if sum>=m:
            list.append(sum-m)
        return
    dfs(u+1,sum+cow_high[u])
    dfs(u+1,sum)

dfs(0,0)
print(min(list))

哆啦A梦的时光机
Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第42张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第43张图片

n = int(input())
def bfs(x,y):
    mx = max(x[0],y)*2
    ans = -1
    seen =set()
    seen.add(x[0])
    queue = [x]
    while queue:
        q = queue.pop(0)
        a = q[0]
        b = q[1]
        if a-1>0 and a-1 not in seen:
            if a-1==y:
                break
            else:
                queue.append([a-1,b+1])
                seen.add(a-1)
        if a+1<mx and a+1 not in seen:
            if a+1==y:
                break
            else:
                queue.append([a+1,b+1])
                seen.add(a+1)
        if a%2==0 and a//2 not in seen:
            if a//2==y:
                break
            else:
                queue.append([a//2,b+1])
                seen.add(a//2)
        if a*2<mx and a*2 not in seen:
            if a*2==y:
                break
            else:
                queue.append([a*2,b+1])
                seen.add(a*2)
    print((b+1)*2)


for i in range(n):
    x,y = map(int,input().split())
    s = [x,0]
    bfs(s,y)

蒜头君回家
Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第44张图片Python算法学习: 计蒜客蓝桥杯训练营练习(持续更新)_第45张图片

def bfs(x,y,end):
    seen = []
    queue = []
    ans = 0
    queue.append([x,y,ans])
    seen.append([x,y])
    while queue:
        now = queue.pop(0)
        for i in range(4):
            now_x = now[0]+dic[i][0]
            now_y = now[1]+dic[i][1]
            if now_x>=0 and now_x<n and now_y>=0 and now_y<m  and  [now_x,now_y] not in seen:
                if home[now_x][now_y]!='#':

                    queue.append([now_x,now_y,now[2]+1])
                    seen.append([now_x,now_y])
                    if  home[now_x][now_y]== end:
                         p_list.append([now_x,now_y,now[2]+1])

if __name__=='__main__':
    n,m =map(int,input().split())
    home =[]
    dic = [[-1,0],[1,0],[0,-1],[0,1]]
    for i in range(n):
        q = input()
        home.append(q)
    for i in range(n):
        for j in range(m):
            if home[i][j]=='S':
                x = i
                y = j
    p_list = []
    ans_list =[]
    bfs(x,y,'P')
    for i in p_list:
        bfs(i[0],i[1],'T')
    for i in range(len(p_list)//2):
        ans = p_list[i][2]+p_list[i+len(p_list)//2][2]
        ans_list.append(ans)
    print(min(ans_list))

你可能感兴趣的:(python,算法)