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]
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
代码验证:
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)
|