2019第十届蓝桥杯C++B组Python解

A组队

2019第十届蓝桥杯C++B组Python解

编号 号位1 2 3 4 5

1 	97 90 0 0 0
2 	92 85 96 0 0
3 	0 0 0 0 93
4 	0 0 0 80 86
5 	89 83 97 0 0
6 	82 86 0 0 0
7 	0 0 0 87 90
8 	0 97 96 0 0
9 	0 0 89 0 0
10 	95 99 0 0 0
11 	0 0 96 97 0
12 	0 0 0 93 98
13 	94 91 0 0 0
14 	0 83 87 0 0
15 	0 0 98 97 98
16 	0 0 0 93 86
17 	98 83 99 98 81
18 	93 87 92 96 98
19 	0 0 0 89 92
20 	0 99 96 95 81

暴力就完事,答案490:

teams=[(97,90,0,0,0),(92,85,96,0,0),(0,0,0,0,93),(0,0,0,80,86),(89,83,97,0,0),
(82,86,0,0,0),(0,0,0,87,90),(0,97,96,0,0),(0,0,89,0,0),(95,99,0,0,0),(0,0,96,97,0),
(0,0,0,93,98),(94,91,0,0,0),(0,83,87,0,0),(0,0,98,97,98),(0,0,0,93,86),(98,83,99,98,81),
(93,87,92,96,98),(0,0,0,89,92),(0,99,96,95,81)]
maxn=0
n=len(teams)
for a in range(n):
    for b in range(a+1,n):
        for c in range(b+1,n):
            for d in range(c+1,n):
                for e in range(d+1,n):
                    tmp=teams[a][0]+teams[b][1]+teams[c][2]+teams[d][3]+teams[e][4]
                    if tmp>maxn:
                        maxn=tmp
print(maxn)
#490

B年号字串

2019第十届蓝桥杯C++B组Python解

类似26进制,模拟即可,答案BYQ

cnt=1
res=[1]
while cnt<2019:
    cnt+=1
    res[0]+=1
    for i in range(len(res)):
        if res[i]>26:
            res[i]-=26
            if i==len(res)-1:
                res.append(1)
            else:
                res[i+1]+=1
            
for i in range(len(res)-1,-1,-1):
    print(chr(64+res[i]),end="")
#BYQ

C数列求值

2019第十届蓝桥杯C++B组Python解

斐波那契数列变形,答案4659

a=1
b=1
c=1
d=3
for i in range(4,20190325):
    d=(a+b+c)%10000
    a=b
    b=c
    c=d
print(d)
#4659

D数的分解

2019第十届蓝桥杯C++B组Python解

注意:要保证不重复,需要i<j<k

答案40785

def check(i):
    if '2' in str(i) or '4' in str(i):
        return False
    return True

cnt=0
for i in range(1,2018):
    if not check(i):
        continue
    for j in range(i+1,2019-i):
        k=2019-i-j
        if k<=j:
            continue
        if check(j) and check(k):
            cnt+=1
print(cnt)
#40785

E迷宫

2019第十届蓝桥杯C++B组Python解

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

深搜即可

答案:

DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

def check(x,y):
    if x >= 0 and x < 30 and y >= 0 and y < 50 and maze[x][y] == '0':
        return True
    return False


def dfs(x,y,pos):
    global best
    global ans
    if pos > best:
        return
    if x == 29 and y == 49:
        tmp = ''
        for i in range(pos):
            tmp += path[i]
        if pos < best:
            ans = tmp
            best = pos
        elif pos == best and tmp < ans:
            ans = tmp
        return
    for i in range(4):
        tmpx = x+dx[i]
        tmpy = y+dy[i]
        if check(tmpx,tmpy) and pos+1<minn[tmpx][tmpy]:
            maze[tmpx][tmpy] = '1'
            minn[tmpx][tmpy] = pos + 1
            path[pos] = d[i]
            dfs(tmpx,tmpy,pos+1)
            maze[tmpx][tmpy] = '0'



with open('maze.txt','r',encoding='utf-8') as file:
    text=file.readlines()

maze=[i.strip() for i in text]
maze=[list(i) for i in maze]

# minn[i][j]记录从起点(0,0)到(i,j)的最短步数
minn = [[float('inf') for i in range(51)] for j in range(31)]
dx = [1,0,0,-1]
dy = [0,-1,1,0]
d = ['D','L','R','U']
path = [''] * 2000
best = float('inf')
ans = ''
dfs(0,0,0)
print(ans)

F特别数的和

2019第十届蓝桥杯C++B组Python解

n=int(input())
s=0
for i in range(1,n+1):
    if '2' in str(i) or '0' in str(i) or '1' in str(i) or '9' in str(i):
        s+=i
print(s)

G完全二叉树的权值

2019第十届蓝桥杯C++B组Python解

2019第十届蓝桥杯C++B组Python解

前i层完全二叉树有pow(2,i)-1个节点,所以将节点按层数加进一个数组,最后遍历一下数组即可

n=int(input())
point=list(map(int,input().split()))
tree=[0]*19
deep=1
for i in range(len(point)):
    if i<pow(2,deep)-1:
        tree[deep]+=point[i]
    else:
        deep+=1
        tree[deep]+=point[i]
maxn=0
res=0
for i in range(len(tree)):
    if maxn<tree[i]:
        maxn=tree[i]
        res=i
print(res)

H等差数列

2019第十届蓝桥杯C++B组Python解

2019第十届蓝桥杯C++B组Python解

思路:数列先排序,找到相邻两数差值的最大公因数就是公差了,需要注意公差为0时的特殊情况,即常数列

from math import gcd

n=int(input())
num=list(map(int,input().split()))
num.sort()
dif=num[1]-num[0]
for i in range(2,len(num)):
    dif=gcd(dif,num[i]-num[i-1])
if dif==0:
    print(n)
else:
    print((num[-1]-num[0])//dif+1)


I后缀表达式

2019第十届蓝桥杯C++B组Python解

看这道题前,我们先来了解一下什么是后缀表达式:

  1. 我们日常生活中常用的是中缀表达式:1+1+2=4,除了中缀表达式之外还有前缀表达式和后缀表达式
  2. 前缀表达式就是把±*/ 都放在前面 比如 ++1 1 2 = 4
  3. 后缀表达式就是把±*/ 都放在后面 比如 1 1 2 ++ = 4
  4. 计算机在计算的时候比较喜欢后缀表达式,人类在计算的时候比较喜欢中缀表达式
  5. 计算机计算后缀表达式的方法是这样的:遇到数字时入栈,遇到±*/时取出栈中的前两个数做运算结果再入栈,反复如此。

这道题有个很神奇的思路:减掉最小值,加上最大值,再加上中间所有数的绝对值,得到的结果就是答案最大值。

当有0个减号时:全部加起来即可

当有1个减号时:肯定选择减最小的那个数,剩下的加起来即可

当有2个减号时:用最小的减去最大的,再用一个减号减去他们的结果,就相当于最大值减最小值,剩下的加起来

当有3个减号时:用最小的减去最大的和第二大的,再用一个减号减去他们的结果,就相当于最大值减最小值,剩下的加起来

……

当有n个减号时:总之留一个减号用来减计算结果,剩下的减号全都用最小的值减较大的值,最后就相当于减掉最小值,加上最大值,再加上中间所有数的绝对值

代码如下:

from math import *

n,m=map(int,input().split())
num=list(map(int,input().split()))
num.sort()
if m==0:
    print(sum(num))
else:
    res=num[-1]-num[0]
    for i in range(1,len(num)-1):
        res+=abs(num[i])
    print(res)

J灵能传输

2019第十届蓝桥杯C++B组Python解

2019第十届蓝桥杯C++B组Python解

2019第十届蓝桥杯C++B组Python解

上一篇:约数


下一篇:AI制作质感蓝色带字水晶球教程