[Three days before the exam] Detailed explanations of the real questions of the Blue Bridge Cup Python Group in the first year of the national competition

foreword

Today, I made a set of national competition questions for the first year of python in the Blue Bridge Cup. I counted 5 choices. It can be said that it is the year of welfare, and this will not happen in the future. At that time, the python group just appeared, and it is estimated that there are many Universities and majors don’t even set up the python course, or some of them are set up. Let’s cut the nonsense and start knocking directly. This time, we won’t look at the source code.
I found that some students are not clear about the competition environment, so I posted a copy of our school machine for reference.
insert image description here

A: Beautiful 2

insert image description here
Count the questions, come up and sign in 3 minutes

ans=0
for i in range(1,2021):
  if str(i).count('2')>0:
    ans+=1
print(ans)

If you want to compete, I will search for the official explanation for everyone
insert image description here

B: Composite number

insert image description here
For this question, write a simple check function to judge whether it is a prime number, and just loop through it.

ans=0
def check(i):
  for j in range(2,i):
    if i%j==0:
      return True
  else:
    return False
for i in range(4,2021):
  if check(i):
    ans+=1
print(ans)

C: factorial divisor

insert image description here
It took me a long time to write this question, and I studied it online for everyone to read.
In short, it is not as simple as the first reaction thinks. It requires the number of positive divisors, and the product of its factors permuted and combined is of course its positive divisors. Prime numbers can also be divisors, and prime numbers can also be it. It is composed of factors. If it takes too long to use each factor to remove the accumulation times one by one, it is still easy to use by decomposition, and the number of combinations between the approximate numbers is their product sum

import os
import sys
# 计算100的阶乘有多少因数
n = 100 # 阶乘的参数
p = [2] # 质数列表,初始化为[2]
# 找出100以内的所有质数,存入p
for i in range(3, n + 1):
    j = 2
    while j < i:
        if i % j == 0: # 如果i能被j整除,说明i不是质数
            break
        j += 1
    else: # 如果i不能被任何小于i的数整除,说明i是质数
        p.append(i)
# print(p) # 输出p,查看质数列表

m = {
    
    } # 质因数字典,用来记录每个质因数的个数
for i in p:
    m[i] = 1 # 初始化为1,代表不选这个质因数的可能

# 遍历从2到100的每个数,对每个数进行质因数分解,并更新m中对应的质因数的个数
for i in range(2, n + 1):
    x = i # x为当前要分解的数
    for j in p: # 遍历质数列表
        if j > x: # 如果j大于x,说明x已经分解完毕,跳出循环
            break
        while x % j == 0: # 如果x能被j整除,说明j是x的一个质因数
            x //= j # 将x除以j,并更新x的值
            m[j] += 1 # 将m中j对应的值加一

s = 1 # 约数个数,初始化为1
# 将m中所有的值相乘,得到约数个数
for i in m.values():
    s *= i

print(s) # 输出约数个数
# 请在此输入您的代码

D: Essence Ascending Sequence

insert image description here
I searched for this question for a long time. At first I thought it was counting the number of occurrences of each letter. Later, I found that the increasing sequence is that each letter only appears once, so we only need to remember the number of each first occurrence. Repeating it doesn't work either. We count from front to back, initialized to 1.

x = "tocyjkdzcieoiodfpbgcncsrjbhmugdnojjddhllnofawllbhfiadgdcdjstemphmnjihecoapdjjrprrqnhgccevdarufmliqijgihhfgdcmxvicfauachlifhafpdccfseflcdgjncadfclvfmadvrnaaahahndsikzssoywakgnfjjaihtniptwoulxbaeqkqhfwl"
dp = [1] * 200						#初始化
for i in range(200):				
    for j in range(i):				#当前字母与它前面的字母进行比较
        if x[i] > x[j]:				#现在出现的字典序大于之前,可以产生新的子序列
            dp[i] += dp[j]
        elif x[j] == x[i]:			#之前已经出现了,把前者产生的子序列数量减掉
            dp[i] -= dp[j]
print(sum(dp))						#sum

Generally speaking, this question is quite complicated, and it is not easy to think about it, because this letter has appeared before, and adding a new one has no effect on the front, so subtract a difference from it, and the subsequent sum will cancel it out

E: toy snake

insert image description here
This question examines the depth recursive dfs, initializes each position in the table once through a loop, and then judges whether it is out of bounds and whether there is a station in four directions.

m=[[0]*4 for i in range(4)]
res=0
def dfs(x,y,cnt):
    global res					#result
    if cnt==16:					#16个位置都走了就结束
        res+=1
        return
    for i,j in [(1,0),(0,-1),(0,1),(-1,0)]:#四个方向
        a,b=x+i,y+j
        if 0<=a<4 and 0<=b<4 and m[a][b]==0:#判断是否可以走
            m[a][b]=1						#站位走下一步
            dfs(a,b,cnt+1)
            m[a][b]=0						#回退到之前
    m[x][y]=0					#回退
    return res
for i in range(4):
    for j in range(4):
        m[i][j]=1
        dfs(x,y,1)
print(res)

F: Heavenly Stems and Earthly Branches

insert image description here
The old method of taking the remainder, first judge the celestial stems and earthly branches of the year of the first test, a cycle of 60 years, 2020%10=0, 2020%12=4

N=int(input())
a=['jia','yi','bing','ding','wu','ji','geng','xin','ren','gui']
b=['zi','chou','yin','mao','chen','si','wu','wei','shen','you','xu','hai']
C=N-4
print(a[C%10]+b[C%12])

Notice! Because there is no space between the two, do not use commas to connect strings, and use plus signs to connect strings.

G: repeat string

insert image description here
When encountering this kind of classification, it is still very fragrant to use dict

k = int(input())
s = list(input())
step = len(s)//k    		# 判断子字符串长度
ans = 0
for i in range(step):
    num = dict()
    for j in s[i::step]:    # 分别遍历每个子字符串的第i位,统计其出现次数,num存储出现次数
        if j in num:
            num[j] += 1		# 这个字母之前出现过num+1
        else:
            num[j] = 1
    ans += k-max(num.values())  # k减去字符串该位出现次数最多的num(最多就是不用换的)
    num.clear()				# 清除这个位置的记录
print(ans)

Looking at the code is a bit abstract, I debugged it, and everyone has a look, if the corresponding substrings are all the same, they will be as big as k, if there is a difference, one must be changed, and the results will be accumulated.
insert image description here

H: Q&A

insert image description here
For this question, I suggest that you push it with a pen first, and the final result is that the smaller the front row, the better.

n=int(input())
li=[]
for i in range(n):
    x,y,z=map(int,input().split())
    A=x+y					#解答时间
    B=x+y+z					#总时间
    li.append([A,B])
li=sorted(li,key=lambda x:x[1],reverse=False)		#用总时间排序
t=0
for i in range(n):
    for j in range(i):				#这层循环添加当前学生之前的学生总时间
        t+=li[j][1]
    t+=li[i][0]						#当前学生的答疑时间
print(t)					#妙!

I: Supplies

insert image description here

J: blue jump jump

insert image description here

epilogue

The last two sisters wrote it out, but it is enough to understand the first eight. Remember to sort out the template three days before the exam, knock every day, find some real questions and bring them in for a try. Finally, I wish you all your wishes come true

Guess you like

Origin blog.csdn.net/weixin_53415043/article/details/129955878