# Solutions to Python group questions in the second game of 2020 Blue Bridge Cup provincial competition

Posted by alluoshi on Thu, 03 Mar 2022 22:14:18 +0100

Turn:

### Article catalogue

• Solutions to Python group questions in the second game of 2020 Blue Bridge Cup provincial Tournament (not quite complete, some can't, some don't remember)
• A house plate making
• Title Description:
• Result: 624
• Solution:
• B 2020？
• Title Description: the second question seems to be the number 2020, right?
• Result: who remembers that? I don't have his data. Woo woo woo
• Solution:
• C serpentine sequence
• Title Description:
• Result: 761 (I seem to have filled in 685 at that time. Did I still fill in wrong? Crying, crying, crying)
• Solution:
• D running training
• Title Description:
• Result: 8879
• Solution:
• E sort, or what's it called
• Title Description:
• Result: I can't solve this problem. Send my, and find a string with a length of 15.
• jonmlkihgfedcba
• Solution:
• F statistical results?
• Title Description:
• Solution:
• G word analysis
• Title Description:
• Example:
• Solution:
• H pick up fruit or something?
• Title Description:
• Sample (I made it up by myself):
• Solution:
• I (unsolved) plane cutting?
• Title Description:
• J (unsolved) monster hunter? (definitely not this name)
• Title Description:
• Solution:

# Solutions to Python group questions in the second game of 2020 Blue Bridge Cup provincial Tournament (not quite complete, some can't, some don't remember)

## A house plate making

### Title Description:

That is to say, the house number is posted on the Internet one by one. For example, 1017 needs two 1s, one 0 and one 7. Then ask how many numbers you need to make 1 to 2020

### Solution:

```# There's nothing to say about direct string lookup
s=0
for i in range(1,2021):# Traverse 1 to 2020
s+=str(i).count('2')# Find how many 2
print(s)
```

## B 2020？

### Title Description: the second question seems to be the number 2020, right?

Just give you a string of 300 * 300 2 and 0, and ask you how many 2020 it contains (this can't use count, because 202020 is 2)

You can only count horizontally from left to right, vertically from top to bottom, or Xie from top to bottom

For example, there are seven in the following, one horizontally, three vertically and three obliquely.

```2 0 2 0 0 2
0 0 0 0 2 0
0 0 2 0 2 2
0 0 0 0 0 0
0 0 0 0 2 0
0 0 0 0 0 0
(I want to make it easy to check. There is no space in the real one.)
```

### Solution:

```# It's a list. I'll split it horizontally and vertically.. It takes a lot of time.
def count(s):
s1=0
for i in range(3,len(s)):# Check 0,1,2,3 - > 1,2,3,4 one by one
if s[i-3:i+1]=='2020':s1+=1# String slice s[0:4] is 0,1,2,3
return s1

with open("2020.txt",'r')as f:
n=len(a)# n line
for i in range(n):# Because it's mi n e.
a[i]=a[i][:-1]
m=len(a)# m column
b=['' for i in range(m)]# b to store these numbers vertically, so there are strings
for i in range(m):
for j in range(n):
b[i]+=a[j][i] # b+=a,a,a,...
c=[]# Slanting is the most troublesome.. My method is to write two cycles with diagonals as the boundary.
for i in range(n-1,-1,-1):# I'm going to find the upper left corner from the lower left corner first
s=''
for j in range(n-i):# Only n-i are added at a time, that is, the length increases from 1 to n (because the length and width are equal)
s+=a[i+j][j]# That is, the first time (n-1,0), the second time (n-2,0 and n-1,1)
c.append(s)
for j in range(1,n):# Change j to traverse from 0 to n, and i remains unchanged initially
s=''
for i in range(n-j):
s+=a[i][i+j]# (0,1)(1,2),...,(n-2,n-1),(0,2)(1,3),...,(0,n-1)
c.append(s)
s=0
for i in a:
s+=count(i)
for i in b:
s+=count(i)
for i in c:
s+=count(i)
print(s)# Traverse the three lists and find out all 2020. In my above example, output 7 is correct!
```

## C serpentine sequence

### Title Description:

The sequence is about as long as this: go right for 2, then go obliquely to the end, then go down for 2, then go obliquely to the end, and then go right for 2

```1  2  6  7 15 16 ..
3  5  8  14 17 ..
4  9  13 18 ..
10 12 19 ..
11 20 ..
21 ..
..
```

Then I ask you, what is row 20 and column 20. (subscript starts from 1)

### Solution:

```# I really simulated how he found it. Later, I heard them say it was regular.
# If   is 1,   is 5,   is 13,   is 25, you will find that 1 and 2 are 4, 2 and 3 are 8, 3 and 4 are 12...
# Because I and j are the same, they are simplified into one dimension. So dp=1,dp[i]=dp[i-1]+4*(i-1)
dp=[0,1]
for i in range(2,21):
print(i-1,dp[-1])
dp.append(dp[-1]+4*(i-1))
print(len(dp)-1,dp[-1])
```

## D running training

### Title Description:

A child runs 1km every day from January 1, 2000 to October 10, 2020, while he runs 2km on Monday or at the beginning of the month (the first day of each month). If you run 2km on Monday and at the beginning of the month. Ask him how many kilometers he ran.

### Solution:

```# I cried to death. I figured out the total number of days on Monday or the beginning of the month. I handed it in directly. I forgot to add the 1km I had to run every day... (I hope I remember wrong, woo woo)
# Oh, here's a trick. You can use dir(datetime) to check his methods and classes.
# In addition, there is a python doc in idle's help, which can also be opened. (I checked the usage method of the datetime class for a long time, resulting in forgetting the title. Woo woo woo)
from datetime import datetime as dt
from datetime import timedelta as td
time1=dt(2000,1,1)# Create start time object
time2=dt(2020,10,1)# Create end time object
day=td(days=1)# Create an object for adding a day. (note that this is td and timedelta)
s=0
while time1<=time2:# Time can be compared directly
if time1.day==1 or time1.weekday()==0: s+=1# weekday() is the method, [0-6], and day is the attribute
s+=1# If it's not special, I also need to add 1. It seems that I forgot to add woo woo woo
time1+=day# Datetime object can only operate with timedelta object and return datetime object.
print(s)

```

## E sort, or what's it called

### Title Description:

That is, if only adjacent elements are exchanged, the exchange times of bubble sorting is the least. For example, lan only exchanges a and l once.

Then let you find the shortest string that needs to be exchanged 100 times in sorting, the one with the smallest dictionary order, and then delete the sentence that can have repeated elements. (I don't know if it means that there can be no duplicate elements)

### Solution:

There is no solution to the blind test.

## F statistical results?

### Title Description:

I don't remember the first two simple programming questions. Maybe the order is confused. This question is, 60 points and above pass, 85 points and above excellent, let you ask for the pass rate and excellent rate. Output in 65% this format, rounded.

### Solution:

```n=int(input())# n students
su,ji,you=0,0,0# Three statistics
for i in range(n):
x=int(input())
su+=1
if x>=60: ji+=1
if x>=85: you+=1
print(f'{ji*100/su:.0f}%')# I output it as a formatted string.
print(f'{you*100/su:.0f}%')
```

## G word analysis

### Title Description:

Hahaha, after reading the problem solution of the Java group next door, I remember this problem. (sure enough, the coincidence rate of Python, Java and C groups is very high 23333)

To get back to business, the main idea of this question is to count which appears most in a string containing only lowercase letters, and output the number of times and who the letters are. If the number of times is the same, the output dictionary order is the smallest.

### Example:

```Input:
lanqiao
Output:
2
a
Input:
longlonglongistoolong
Output:
6
o
```

### Solution:

```s=input()
m1,m2=0,0 # m1 represents the maximum number of times and m2 represents the corresponding letter
for i in range(25):
c=chr(97+i) # Ergodic a-z
t=s.count(c) # Find the number of times a letter appears
if t>m1: # Here, it is necessary to be greater than or equal to or not change, so as to ensure the smallest dictionary order.
m1=t
m2=c
print(m1)
print(m2)
```

## H pick up fruit or something?

### Title Description:

A child picks up fruit layer by layer. Each layer can only go to the nearest one at the bottom left and right. Ask how many you can pick up at most. The difference between the times of going left and going right cannot exceed 1. Alas, it's a pity that I didn't understand the question at that time.. Also wrote a thief for a long time..

### Sample (I made it up by myself):

```Input:
4
7
9 10
9 6 9
2 6 10 9
Output:
36
Tips:
Going left means going down, and going right means going down to the right.
The example is lower right 7+10+9+10=36.
```

### Solution:

I've been struggling with this problem for a long time because I didn't understand it. After the competition, I worked out this problem...

```# The following two solutions will be written at the same time: recursive version (timeout) and dynamic programming version. (recursive version is used to judge whether the shooting is correct or not)
ss=0
def dfs(i,j,l,r,s):
# Recursion to the last floor: if
if i==n-1:
# If the difference between the number of left and right walks exceeds 1, return directly
if abs(l-r)>1: return
# Otherwise, ss takes the larger one. (since s starts from 0, add a[i][j] each time)
global ss
ss=max(ss,s+a[i][j])
return
# In the case of recursive left and right walking, left walking is i+1, j remains unchanged, l+1,
dfs(i+1,j,l+1,r,s+a[i][j])
# Go right, i+1, j + 1, R + 1 a[i][j] should be added to s unification to represent the sum of the current step. (not the sum to the next step)
dfs(i+1,j+1,l,r+1,s+a[i][j])

# Using a self-made example, the maximum n is 100
for ww in range(16):
with open(f"{ww+1}.in",'r')as f:
# --------Input data
a=[[] for i in range(n)]# Initialize n lists
for i in range(n):# Each list directly inherits the input of each line (it seems that it is directly equal to it, cover your face)
# --------Calculation results
# Recursive version of the solution, violence against the beat, to my fourth example, even if it doesn't move, it will probably reach n = 30 or 40?
# Each time we go to the last layer, compare SS with the result. Who is the bigger ss? Take the bigger SS, and finally output SS.
# ss=0
# The five parameters are i,j,l,r,s.i and j are coordinates, l and r are the times of walking left and right, and S is the result of sum.
# dfs(0,0,0,0,0)
# print(ss)
# The following is the dynamic programming solution
# Initialize a shape, such as [, [0,0], [0,0,0],...] dp array of
dp=[[0 for j in range(i+1)] for i in range(n)]
dp=a # The starting point is a
# Traverse each layer
for i in range(1,n):
# If j is 0, it can only come down from the 0 of the upper layer, so dp[i-1]+a[i] is the result of this layer.
dp[i]=dp[i-1]+a[i]
# Then j traverses [1,i-1]
for j in range(1,i):
# At this time, there are two possibilities, which may be walking down from top left to right, or walking down from top right to left.
# It is necessary to compare the larger of them and add a[i][j].
dp[i][j]=max(dp[i-1][j-1],dp[i-1][j])+a[i][j]
# Similarly, the last one on each floor can only be the last one on the upper floor.
dp[i][i]=dp[i-1][i-1]+a[i][i]
# You may think, if so, how do you know how many times he walked left and right at the last floor?
# This is actually regular. Because the difference between his left and right cannot exceed 1, he can only go to the following two points at most.
# And whether n is odd or even. If n is an odd number, he walks left and right the same number of times. (because the first floor is step 0)
# So at this time, he can only go to dp[-1][n//2]. (because J remains the same on the left, j+1 on the right, and half the time on the right. It's in the middle anyway.)
if n%2: print(dp[-1][n//2])
# If n is an even number, it can only be left one more time or right one more time, right one more time is n//2, and right one less time is n//2-1
else: print(max(dp[-1][n//2],dp[-1][n//2-1]))

```

## I (unsolved) plane cutting?

### Title Description:

It seems to give you some k and b, y=kx+b, and then let you judge how many parts the plane will be divided by these lines.

## J (unsolved) monster hunter? (definitely not this name)

### Title Description:

It's too long to remember.

### Solution:

I can't remember clearly. I only remember that in the last few minutes, I barely wrote an answer that is missing 100 million details but can pass the example.

Turn:

### Solutions to Python group questions in the second game of 2020 Blue Bridge Cup provincial competition

--Posted from Rpc