试题A:门牌制作
s = 0 for i in range(1,2021): s += str(i).count('2') print(s) # 624
试题B:寻找2020
nums = [[2, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 2, 2, 0, 2], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 2], [0, 0, 2, 0, 2, 0], ] n = 0 for i in range(len(nums)): for j in range(len(nums[0])): if j+4 <= len(nums[0]): a = nums[i][j:j+4] if a == [2,0,2,0]: n+=1 if i+3<len(nums): b = nums[i][j]*1000+nums[i+1][j]*100+nums[i+2][j]*10+nums[i+3][j] if b == 2020: n+=1 if i+3<len(nums) and j+3 < len(nums[0]): c = nums[i][j]*1000+nums[i+1][j+1]*100+nums[i+2][j+2]*10+nums[i+3][j+3] if c == 2020: n+=1 print(n)
试题C:跑步锻炼
import datetime a = datetime.date(2000,1,1) b = datetime.date(2020,10,2) num = 0 while True: if a == b: break if a.day == 1: num+=2 a = a+datetime.timedelta(days=1) continue if a.isoweekday() == 1: num+=2 a = a + datetime.timedelta(days=1) continue num+=1 a = a + datetime.timedelta(days=1) print(num) # 8879
试题D:蛇形填数
# 代码运行 lst = [[] for i in range(40)] num = 1 lst[0].append(1) for i in range(2, 40): if i % 2 == 0: for j in range(i): num += 1 lst[j].append(num) else: for j in range(i): num += 1 lst[i - j - 1].append(num) for i in range(len(lst)): print(i + 1, lst[i]) print(lst[19][19])
试题E: 排序
# 当所有的字符全全部倒序的时候重新排序的次数就越多,所用的字符就越少 # 又因为要求字典序最小,所以字母应该是前n位 # 由于冒泡排序的特征,完全倒序的情况下,交换的次数是(n-1)+(n-2)......2+1 # 算出当有15字符完全倒序的时候为,105,所以,只需要在此基础上稍加修改即可, # 又因为需要保证字典序最小,即字符串的第一个字目应该尽量的小,所以将j前提5个字符 # 'abcdefghijklmno' # print('jonmlkihgfedcba')
试题F:成绩统计
n = int(input()) y = 0 j = 0 for i in range(n): num = int(input()) if num >= 85: y+=1 if num >=60: j +=1 print('{:.0f}%'.format((j/n)*100)) print('{:.0f}%'.format((y/n)*100))