Python【2020 Blue Bridge Cup Provincial Game 2 Fill in the Blank】

Python【2020 Blue Bridge Cup Provincial Game 2 Fill in the Blank】

A. House number production

insert image description here

Code:

ans = 0
for i in range(1,2021):
    now = i
    while now:
        if now % 10 == 2:
            ans += 1
        now //= 10
print(ans)

Answer: 624

B. Looking for 2020

insert image description here

document:

Python [Looking for 2020.txt] - Alan_Lowe's Blog - CSDN Blog

Code:

s, ans = [], 0
x = input()
while x:
    s.append(x)
    x = input()
for i in range(0,len(s)):
    for j in range(0,len(s[i])):
        if s[i][j] == '2':
            if j + 3 < len(s[i]) and s[i][j + 1] == '0' and s[i][j + 2] == '2' and s[i][j + 3] == '0':
                ans += 1
            if i + 3 < len(s) and s[i + 1][j] == '0' and s[i + 2][j] == '2' and s[i + 3][j] == '0':
                ans += 1
            if j + 3 < len(s[i]) and i + 3 < len(s) and s[i + 1][j + 1] == '0' and s[i + 2][j + 2] == '2' and s[i + 3][j + 3] == '0':
                ans += 1
print(ans)

Answer: 16520

C. Running exercise

insert image description here

Code:

y, m, d, xingqi, ans = 2000, 1, 1, 6, 0         # 表示当前日期、星期、答案
month = [0,31,28,31,30,31,30,31,31,30,31,30,31] # 表示每个月有多少天
# 判断该年,每年都将2月的天数做修改
def is_leap(yyy):
    if (yyy % 4 == 0 and yyy % 100 != 0) or yyy % 400 == 0:
        month[2] = 29
    else:
        month[2] = 28
# 到下一天
def next_day(yy,mm,dd,xingqi):
    is_leap(yy)     # 改第二个月的天数
    xingqi += 1     # 改星期几
    if xingqi > 7:
        xingqi = 1
    dd += 1         # 改日期
    if dd > month[mm]:
        dd = 1
        mm += 1
    if mm > 12:
        mm = 1
        yy += 1
    return [yy,mm,dd,xingqi]
# 遍历每一天
while not(y == 2020 and m == 10 and d == 2):
    if d == 1 or xingqi == 1:
        ans += 2
    else:
        ans += 1
    y, m, d, xingqi = next_day(y,m,d,xingqi)
print(ans)

Answer: 8879

D. Snake fill

insert image description here

Ideas:

通过观察可以看出第i行第i列的前面有x = 2*(i-1)个斜行,[i,i]位于所在斜行的第i个位置
所以(x + 1) * x / 2 + i

Answer: 761

E. Sorting

insert image description here

Ideas:

# abcdefghijklmnopqrstuvwxyz
# lan -> aln 交换1次,逆序数为1
# qiao -> aioq 交换4次,逆序数为4
# dcba -> 逆序数为3 + 2 + 1
# 需要100次 如果位数为14位:13 + 12 + ... + 1 = 91 不够
# 如果位数位15位:14 + 13 + ... + 1 = 105够了
# 所以最短应该是15位
# 多了5个,那就把第6位提到最前面就可以了
# onmlkjihgfedcba(105) -> jonmlkihgfedcba

Code verification:

# abcdefghijklmnopqrstuvwxyz
# lan -> aln 交换1次,逆序数为1
# qiao -> aioq 交换4次,逆序数为4
# dcba -> 逆序数为3 + 2 + 1
# 需要100次 如果位数为14位:13 + 12 + ... + 1 = 91 不够
# 如果位数位15位:14 + 13 + ... + 1 = 105够了
# 所以最短应该是15位
# 多了5个,那就把第6位提到最前面就可以了
# onmlkjihgfedcba(105) -> jonmlkihgfedcba
s = ['j', 'o', 'n', 'm', 'l', 'k', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
ans = 0
for i in range(0,len(s)):
    for j in range(len(s) - 1, 0 ,-1):
        if s[j] < s[j - 1]:
            ans += 1
            ch = s[j]
            s[j] = s[j - 1]
            s[j - 1] = ch
print(ans)

Guess you like

Origin blog.csdn.net/qq_45985728/article/details/123979727
Recommended