A门牌制作
答案:624
代码:
total = 0
for i in range (1,2021):
s = str(i).count('2')
total += s
print(total)
B寻找2020
答案:16520
代码:暴力
l = []
with open('2020.txt') as fp:
for line in fp.readlines():
l.append(list(line.strip()))
s = len(l) #301
q = len(l[1])#300
count = 0
for i in range(s-1): #行
for j in range(q-3):
if (l[i][j] == '2' and l[i][j+1] == '0' and l[i][j+2]=='2' and l[i][j+3]=='0'):
count = count+1
for i in range(q-3): #列
for j in range(s-1):
if (l[i][j] == '2' and l[i+1][j] == '0' and l[i+2][j]=='2' and l[i+3][j]=='0'):
count = count+1
for i in range(q-3): #左上到右下
for j in range(q-3):
if (l[i][j] == '2' and l[i+1][j+1] == '0' and l[i+2][j+2]=='2' and l[i+3][j+3]=='0'):
count = count+1
print(count)
C 跑步锻炼
答案:8879
import datetime
def week(year, month, day): # 输入年月日判断是星期几
someday = datetime.date(year, month, day)
return int(someday.strftime('%w'))
def Isleap_year(x): # 判断是否是闰年
if x % 4 == 0 and x % 100 != 0 or x % 400 == 0:
return True
else:
return False
r_n = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
p_n = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
totalkm = 0
for n in range(2000, 2021):
count = 0
mcount = 0
if n == 2020:
for y in range(1, 10):
for r in range(1, r_n[y] + 1):
if week(n, y, r) == 1:
mcount = mcount + 1
if week(n, y, r) == 1 and r == 1:
count = count + 1
run_km = (10 + mcount - count) * 2 + 274 - (10 + mcount - count)
#print(n, mcount, count, run_km)
elif Isleap_year(n):
for y in range(1, 13):
for r in range(1, r_n[y] + 1):
if week(n, y, r) == 1:
mcount = mcount + 1
if week(n, y, r) == 1 and r == 1:
count = count + 1
run_km = (12 + mcount - count) * 2 + 366 - (12 + mcount - count)
#print(n, mcount, count, run_km)
else:
for y in range(1, 13):
for r in range(1, p_n[y] + 1):
if week(n, y, r) == 1:
mcount = mcount + 1
if week(n, y, r) == 1 and r == 1:
count = count + 1
run_km = (12 + mcount - count) * 2 + 365 - (12 + mcount - count)
#print(n, mcount, count, run_km)
totalkm = totalkm + run_km
print(totalkm)
E 排序
思路 :看他的条件,这个字符串没有字母重复且最短
相同长度的字符串经过冒泡从小到大排序交换次数最多的肯定是他的逆序
像 abcd :如果是abdc 交换次数是1, 如果是dcba 交换次数就是6
所以我们就先大范围尝试 从a ,ba,cba,dcba…一直到次数大于等于100的那个字符
s = input()
l = list(s)
def bubble_sort(l):
n = len(l)
count = 0
for j in range(0,n-1):
for i in range(0,n-1-j):
if l[i]>l[i+1]:
l[i],l[i+1]=l[i+1],l[i]
count = count+1
return l,count
print(bubble_sort(l))
然后我们调整一下o的位置
答案:nmlkjoihgfedcba
F 成绩统计
n = int(input())
l = []
for i in range(n):
score = int(input())
l.append(score)
jg = 0
yx = 0
for i in l:
if i >=85:
yx = yx+1
for i in l:
if i>=60:
jg = jg+1
print("{:.0%}".format(jg/n))
print("{:.0%}".format(yx/n))
G单词分析
s = input()
l = list(s)
max_count = 0
k = ''
for i in range(len(l)):
cou = l.count(l[i])
if cou>max_count:
max_count = cou
k = l[i]
print(k)
print(max_count)
H数字三角形
n = int(input())
a = [[0 for j in range(2*n-1)]for i in range(n)]
for i in range(n):
l = list(map(int,input().split()))
k = 0
for j in range(n-i-1,n-i-1+i*2+1,2):
a[i][j] = l[k]
k = k+1
b = [[0 for j in range(2*n-1)]for i in range(n)]
b[0] = a[0]
for i in range(1,n):
for j in range(2*n-1):
if (j-1>=0 and j+1 < 2*n-1):
b[i][j] = max(b[i-1][j-1],b[i-1][j+1])+a[i][j]
elif (j-1>=0):
b[i][j] =b[i-1][j-1] + a[i][j]
elif (j+1 < 2*n-1):
b[i][j] =b[i-1][j+1] + a[i][j]
print(max(b[n-1]))
I 平面切分