The first real question of the 12th Blue Bridge Cup Provincial Competition [Part]

The first real question of the 12th Blue Bridge Cup Provincial Competition [Part]

1. Time display

insert image description here

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

format Common format processing:

保留小数:
a = 7
b = 3
print("{:.2f}".format(a / b))
print("{:.3f}".format(3.5458))
print("{:.3f}".format(3.5454))
print("{:.3f}".format(3.5455))
输出:
2.33
3.546
3.545
3.546
----------------------------------------------------
补齐:
print('{:>5d}'.format(12))
print('{:>5d}'.format(1234))
print('{:>5d}'.format(12345))
print('{:>5d}'.format(1))
print('---------------分割线')
print('{:x<5d}'.format(12))
print('{:x<5d}'.format(1234))
print('{:x<5d}'.format(12345))
print('{:x<5d}'.format(1))
输出:
   12
 1234
12345
    1
---------------分割线
12xxx
1234x
12345
1xxxx
----------------------------------------------------
逗号分隔数:
print('{:,}'.format(1999888565478))
输出:
1,999,888,565,478
----------------------------------------------------
百分比:
print('{:%}'.format(0.3785))
print('{:.1%}'.format(0.3785))
输出:
37.850000%
37.9%
----------------------------------------------------
居中对齐:
print('{:^10d}'.format(1))
print('{:^10d}'.format(123))
print('{:^10d}'.format(12))
print('{:^10d}'.format(12345))
print('{:^10d}'.format(1234))
输出:
    1     
   123    
    12    
  12345   
   1234   

2. Weighing with weights

insert image description here

n = int(input())
w = list(map(int,input().split()))
dp = [0 for i in range(0,100005)]
dp[0] = 1
for i in w:
    j = 100000
    while j >= i:
        dp[j] = max(dp[j],dp[j - i])
        j -= 1

for i in w:
    for j in range(1,100001 - i):
        dp[j] = max(dp[j],dp[j + i])

print(sum(dp) - 1)

3. Yang Hui triangle

insert image description here

Ideas:

insert image description here

insert image description here

n = int(input())

def C(a,b):                 #组合数
    sum = 1
    for i in range(1,b + 1):
        sum *= a
        sum //= i
        a -= 1
        if sum > n:
            return sum
    return sum

def check(kk):              # 二分
    l = 2 * kk
    r = max(l,n)
    while l < r:
        mid = l + r >> 1
        if C(mid,kk) >= n:
            r = mid
        else:
            l = mid + 1
    if C(r,kk) == n:
        print((r + 1) * r // 2 + kk + 1)
        return True
    return False

if n == 1:                  # 特判
    print(1)
else:
    k = 16
    while k > 0:            # 枚举
        if check(k):
            break
        k -= 1

Guess you like

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