Python【2020蓝桥杯省赛第二场填空】

Python【2020蓝桥杯省赛第二场填空】

A.门牌制作

在这里插入图片描述

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

B.寻找2020

在这里插入图片描述

文件:

Python【寻找2020.txt】_Alan_Lowe的博客-CSDN博客

代码:
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)
答案:16520

C.跑步锻炼

在这里插入图片描述

代码:
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)
答案:8879

D.蛇形填数

在这里插入图片描述

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

E.排序

在这里插入图片描述

思路:
# 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
代码验证:
# 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)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alan_Lowe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值