The real questions of the 12th Python Group of the Blue Bridge Cup

content

1. Cards

2.40257

3.

4.10266837

5.

6. Time display

7. Yang Hui Triangle 

8. 

9.

10.


After doing it for three hours, compared with last year, you can do one more dijkstra

In other places, there is no idea at all. I looked at the answers and they are all moving, dfs, bfs, but with my current understanding, it seems that I can't use it.

This time, it's time to experience the topic. In the next two months, the focus will be on returning and searching.

1. Cards

The answer is 3181

hash=[0 for i in range(10)]
i=1
while(i):
    string=str(i)
    for j in string:
        hash[int(j)]+=1
        if hash[int(j)]==2021:
            print(i)
            i=0
            break
    if i==0:
        break
    else:i+=1

2.40257

python decimal arithmetic will be funny 

def getline(a,b):
    if b[0]-a[0]==0:
        k=-1<<31
        b=b[0]
    else:
        k=(b[1]-a[1])/(b[0]-a[0])
        b=b[1]-k*b[0]
    return (k,b)
point=[]
for i in range(20):
    for j in range(21):
        point.append((i,j))
ans=[]
for i in range(len(point)):
    for j in range(i+1,len(point)):
        line=getline(point[i],point[j])
        if line not in ans:
            ans.append(line)
print(len(ans))

3.

4.10266837

def gcd(a,b):
    if b==0:
        return a
    return gcd(b,a%b)
def getmin(a,b):
    return a*b//gcd(a,b)
adj=[[]for i in range(2022)]
for i in range(1,2022):
    left,right=-1,-1
    if i<21:
        left=1
        right=i+21
    elif i>2000:
        left=i-21
        right=2021
    else:
        left=i-21
        right=i+21
    for j in range(left,right+1):
        length=getmin(i,j)
        adj[i].append((j,length))
d=[1<<31 for i in range(2022)]
vis=[False for i in range(2022)]
def dijkstra():
    d[1]=0
    for i in range(2021):
        minlength=1<<31
        minindex=-1
        for j in range(1,2022):
            if not vis[j] and d[j]<minlength:
                minlength=d[j]
                minindex=j
        if minindex==-1:
            return
        vis[minindex]=True
        for point in adj[minindex]:
            index=point[0]
            length=point[1]
            if not vis[index] and d[index]>d[minindex]+length:
                d[index]=d[minindex]+length
dijkstra()
print(d[2021])

5.

def gcd(a,b):
    if b==0:
        return a
    return gcd(b,a%b)
adj=[[]for i in range(22)]
for i in range(1,22):
    for j in range(i+1,22):
        if gcd(i,j)==1:
            adj[i].append(j)
            adj[j].append(i)

vis=[False for i in range(22)]
ans=0
length=0
def dfs(cur):
    global vis
    global ans
    global length
    if 1 in adj[cur] and False not in vis:
        ans+=1
        return
    if False not in vis:
        return
    for i in adj[cur]:
        if not vis[i]:
            vis[i]=True
            dfs(i)
            vis[i]=False
    return
# for i in adj:
#     print(i)
vis[0]=True
vis[1]=True
dfs(1)
print(ans)

6. Time display

a=int(input())
day=24*60*60
s=a//1000
s=s%day
h=s//3600
lst=s%3600
m=lst//60
s=lst%60
print("{:02d}:{:02d}:{:02d}".format(h,m,s))

7. Yang Hui Triangle 

a=int(input())
dp=[[0 for i in range(a)]for i in range(a)]
dp[0][0]=1
cur=1
def finds():
    for i in range(1,a):
        dp[i][0]=1
        dp[i][i]=1
        for j in range(1,i):
            dp[i][j]=dp[i-1][j-1]+dp[i-1][j]
            if dp[i][j]==a:
                cur=(i*(i+1))//2+1+j
                return cur
print(finds())

8. 

9.

10.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324093305&siteId=291194637