Python [2021 Blue Bridge Cup Provincial Competition Programming Questions]

Python [2021 Blue Bridge Cup Provincial Competition Programming Questions]

F. Time display

insert image description here

n = int(input()) // 1000 % 86400
h = n // 3600
m = n % 3600 // 60
s = n % 60
print(str("{:0>2d}".format(h)) + ":" + str("{:0>2d}".format(m)) + ":" + str("{:0>2d}".format(s)),end='')

G. Yang Hui triangle

insert image description here

import math

n = int(input())

def C(mid,i):
    ans = 1
    for ii in range(1,i + 1):
        ans *= (mid - ii + 1)
        ans /= ii
        if ans > n:
            return ans
    return ans

def check(i):
    l, r, mid = 2 * i, max(n,2 * i), 0
    while l < r:
        mid = l + r >> 1
        if C(mid,i) < n:
            l = mid + 1
        else:
            r = mid
    if C(l,i) == n:
        print((l + 1) * l // 2 + i + 1,end='')
        return True
    return False

if n == 1:
    print(1,end='')
else:
    ii = 16
    while ii > 0:
        if check(ii):
            break
        ii-=1

Ah, this question can't be passed with the number of combinations that comes with python. . . . . . . . .

H. Left child right brother

insert image description here

n = int(input())
# 建树
tree = [[] for i in range(0,n + 5)]
for i in range(2,n + 1):
    tree[int(input())].append(i)

def dfs(now):
    mn = 0
    for i in tree[now]:
        mn = max(mn,dfs(i))
    return mn + len(tree[now])
print(dfs(1))

I don't know why ACwing always reports RE with dfs.

n = int(input())
f = [0 for i in range(n + 5)]
c = [0 for i in range(n + 5)]
dp = [0 for i in range(n + 5)]
for i in range(2,n + 1):
    x = int(input())
    f[i] = x
    c[x] += 1
for i in range(n, 0, -1):
    dp[f[i]] = max(dp[f[i]],dp[i] + c[f[i]])
print(dp[1])

This can be done without recursion. . .

I. XOR sequence

insert image description here

t = int(input())
for _ in range(0,t):
    x = list(map(int,input().split()))
    y = [0 for i in range(25)]
    # 将每个数都分离成二进制
    for i in range(1,len(x)):
        now = 0
        while x[i] > 0:
            y[now] += x[i] % 2
            x[i] //= 2
            now += 1
    # 判断胜负
    i = 20
    flag = 1
    while i >= 0:
        if y[i] % 2 == 0:
            i -= 1
            continue
        else:
            if y[i] == 1:
                print('1')
                flag = 0
                break
            if x[0] % 2 == 1:
                print('1')
                flag = 0
                break
            else:
                print('-1')
                flag = 0
                break
    if flag == 1:
        print('0')

J. Bracket Sequence

insert image description here

More later. . .

Guess you like

Origin blog.csdn.net/qq_45985728/article/details/123971796