一、门牌制作
题目
思路
模拟
代码
ans = 0
for i in range(1,2021):
ans += str(i).count("2")
print(ans)
二、卡片
题目
思路
数字1最先使用,所以只需注意什么时候数字1使用完
代码
remain = 2021
start = 1
while True:
remain -= str(start).count("1")
if remain == 0:
break
start += 1
print(start)
三、迷宫
题目
思路
DFS
代码
temp = """UDDLUULRUL
UURLLLRRRU
RRUURLDLRD
RUDDDDUUUU
URUDLLRRUU
DURLRLDLRL
ULLURLLRDU
RDLULLRDDD
UUDDUDUDLL
ULRDLUURRR"""
temp = temp.split("\n")
sign = [[0]*10 for i in range(10)]
## 1已经走过 0还未走过
def init():
global sign
sign = [[0]*10 for i in range(10)]
def dfs(a,b):
if sign[a][b] == 1:
return False
sign[a][b] = 1
if temp[a][b] == "U":
nex,ney =a-1,b
elif temp[a][b] == "D":
nex,ney = a+1,b
elif temp[a][b] == "L":
nex,ney = a,b-1
else:
nex,ney = a,b+1
if nex < 0 or nex > 9 or ney < 0 or ney >9:
return True
else:
return dfs(nex,ney)
ans = 0
for i in range(10):
for j in range(10):
init()
if dfs(i,j):
## print(f"{i},{j} 1")
ans += 1
print(ans)
四、七段码
题目
详细见我的另一篇文章
思路
itertools组合+并查集
代码
import itertools
a = {0:[1,5],
1:[0,2,6],
2:[1,3,6],
3:[2,4],
4:[3,5,6],
5:[0,4,6],
6:[1,2,4,5]}
def lian(n):
fa = [_ for _ in range(7)]
def find(a):
if fa[a] == a:
return a
else:
fa[a] = find(fa[a])
return fa[a]
def union(a,b):
a_fa = find(a)
b_fa = find(b)
fa[a_fa] = b_fa
if len(n) == 1:
return True
for i in n:
for j in a[int(i)]:
if str(j) in n:
union(int(i),j)
for i in n:
if find(int(n[0])) != find(int(i)):
return False
return True
ans = 0
for length in range(1,8):
for i in itertools.combinations("0123456",r = length):
if lian("".join(i)):
ans += 1
print(ans)
五、分数
题目
思路
等比数列求和公式,模拟
代码
import math
if math.gcd(pow(2,20)-1,pow(2,19)) == 1:
print("%d/%d"%(pow(2,20)-1,pow(2,19)))
else:
print("%d/%d"%(pow(2,20)//math.gcd(pow(2,20),pow(2,19))-1,pow(2,19)//math.gcd(pow(2,20),pow(2,19))))
六、星期一
题目
思路
datetime模块
代码
import datetime
start = datetime.date(1901,1,1)
end = datetime.date(2000,12,31)
step = datetime.timedelta(days = 1)
ans = 0
while start <= end:
if start.isoweekday() == 1:
ans += 1
start += step
print(ans)
七、顺子日期
题目
思路
datetime模块+模拟
代码
import datetime
start = datetime.date(2022,1,1)
end = datetime.date(2022,12,31)
step = datetime.timedelta(days = 1)
def check(a):
for i in range(len(a)-2):
if int(a[i])+1 == int(a[i+1]) and int(a[i+1])+1 == int(a[i+2]):
return True
return False
ans = 0
while start <= end:
if check(start.strftime("%Y%m%d")):
ans += 1
start += step
print(ans)
八、乘积尾零
题目
思路
字符串rstrip(“0”),每一步都把零去掉
代码
temp = """5650 4542 3554 473 946 4114 3871 9073 90 4329
2758 7949 6113 5659 5245 7432 3051 4434 6704 3594
9937 1173 6866 3397 4759 7557 3070 2287 1453 9899
1486 5722 3135 1170 4014 5510 5120 729 2880 9019
2049 698 4582 4346 4427 646 9742 7340 1230 7683
5693 7015 6887 7381 4172 4341 2909 2027 7355 5649
6701 6645 1671 5978 2704 9926 295 3125 3878 6785
2066 4247 4800 1578 6652 4616 1113 6205 3264 2915
3966 5291 2904 1285 2193 1428 2265 8730 9436 7074
689 5510 8243 6114 337 4096 8199 7313 3685 211 """
temp = temp.split("\n")
for i in range(len(temp)):
## print(list(i.split()))
temp[i] = list(temp[i].split())
ans = 0
n = "1"
for i in range(10):
for j in range(10):
n = str(int(n)*int(temp[i][j]))
ans += len(n)-len(n.rstrip("0"))
n = n.rstrip("0")
print(ans)
九、平方和
题目
思路
模拟
代码
ans = 0
for i in range(1,2020):
if "2" in str(i) or "0" in str(i) or "1" in str(i) or "9" in str(i):
ans += i**2
print(ans)
十、空间
题目
思路
模拟
代码
## uniy all to byte
## MB
temp = 256
## to KB
temp *= 1024
## to B
temp *= 1024
## to b
temp *= 8
print(temp//32)
十一、ASC
题目
思路
模拟
代码
A_n = 65
print(A_n + 12-1)
十二、特殊时间
题目
思路
月日的限制最大,时分的限制次之,年份限制无,大中小循环分割再组合
代码
day = []
hm = []
def daycheck(a):
temp = set([i for i in a])
if ([a.count(k) for k in temp] == [1,3] or [a.count(k) for k in temp] == [3,1]) and a != "1131":
return True
return False
def hmcheck(a):
if a.count("1") == 3 or a.count("2") == 3:
return True
return False
for m in range(1,13):
for d in range(1,32):
s = f"{m:0>2}{d:0>2}"
if daycheck(s):
day.append("".join(sorted(s)))
for h in range(0,24):
for i in range(0,60):
s = f"{h:0>2}{i:0>2}"
if hmcheck(s):
hm.append("".join(sorted(s)))
ans = 0
for i in day:
for j in hm:
if i == j:
ans += 1
print(ans*4)
反面教材
一股脑丢进去,所有循环、判断都堆叠在一起,效率很低!短时间根本跑不出!
def check(a):
if a[4:8] == "1131":
return False
b = a[:4]
c = a[4:8]
d = a[8:]
b_set = "".join(sorted(list(set(i for i in b))))
b_n = "".join([str(b.count(i)) for i in b_set])
c_set = "".join(sorted(list(set(i for i in c))))
c_n = "".join([str(c.count(i)) for i in c_set])
d_set = "".join(sorted(list(set(i for i in d))))
d_n = "".join([str(d.count(i)) for i in d_set])
if b_set == c_set == d_set and b_n == c_n == d_n:
return True
return False
ans = 0
for y in range(1,10000):
for m in range(1,13):
for d in range(1,31):
for h in range(1,24):
for i in range(1,60):
if check(f"{y:0>4}"+f"{m:0>2}"+f"{d:0>2}"+f"{h:0>2}"+f"{i:0>2}"):
ans += 1
print(ans)
学长代码
这里我想不到只判断1、2的个数,其实自己在草稿纸上写一下年月就可以发现,三个的只会是1或者2,还有就是注意到日这里学长直接只遍历1-30,自动把11.31这个月日排除了,如果自己循环没有剔除掉11.31就要自己重新写check函数。
def check(n):
if n.count("1") == 3 or n.count("2") == 3:
return True
return False
day = []
for m in range(1,13):
for d in range(1,31):
s = "%02d%02d"%(m,d)
if s == "1131":
print("chuxian")
print(day)
s1 = sorted(s)
if check(s):
day.append(s1)
hour = []
for h in range(0,24):
for m in range(0,60):
s = "%02d%02d"%(h,m)
s1 = sorted(s)
if check(s):
hour.append(s1)
cnt = 0
for j in day:
for k in hour:
if j == k:
cnt += 1
print(cnt*4)
十三、相乘
题目
思路
大小循环中选小循环
代码
for i in range(0,2021):
if (i*1000000007+999999999)%2021 == 0:
print((i*1000000007+999999999)//2021)
收获:
- 局部变量和全局变量的细节
- 判断的DFS不能单纯dfs()而是return dfs()
- 尽量不要把所有循环堆叠,可以将循环拆分然后组合
- 要细心!要细心!要细心!