1.卡片#统计1累计出现的次数
val=0
num=0
while True:
num+=str(val).count('1')
if num>=2021:
print(val)
break
val+=1
2.直线#解析参考这位博主
#直线
x=[i for i in range(20)]
y=[i for i in range(21)]
zuobiao=set()
for i in x:
for j in y:
zuobiao.add((i,j))
zuobiao=sorted(zuobiao)
kb=set()
Tong=set()
for i in range(len(zuobiao)):
for j in range(i+1,len(zuobiao)):
if zuobiao[i][0] !=zuobiao[j][0]:#计算斜率和截距
k=(zuobiao[i][1]-zuobiao[j][1])/(zuobiao[i][0]-zuobiao[j][0])
b=(zuobiao[j][0]*zuobiao[i][1]-zuobiao[i][0]*zuobiao[j][1])/(zuobiao[j][0]-zuobiao[i][0])
# b = (x2 * y1 - x1 * y2) / (x2 - x1) # 截距公式——*建议加强记忆*
kb.add((k,b))
else:
Tong.add(zuobiao[i][0])
print(len(kb)+len(Tong))
3.货物摆放#暴力
货物摆放
import math
n = 2021041820210418
num=0
chushu=set()
for i in range(1,int(math.sqrt(n))+1):
if n%i==0:
chushu.add(i)
chushu.add(n/i)
for i in chushu:
for j in chushu:
for k in chushu:
if i*j*k==n:
num+=1
print(num)
4.路径#动态规划
#路径 #动态规划
# import math
# nn=2025
# dp=[math.inf]*2025
# dp[1]=0
# for i in range(1,2025):
# for j in range(i+1,min(nn,i+22)):
# dp[j]=min(dp[j],i*j/math.gcd(i,j)+dp[i]) #dp[i]=min(dp[i],dp[i,j]+dp[j])
# print(dp[2021])
5.回路计数#回溯可以看看
#回路计数#回溯法
import math
def backroad(path):
if len(path)==21:
Length+=1
return
for num in range(2,22):
if len(path)==1:
path.append(num)
backroad(path,ss)
path.pop()
else:
if num in path or math.gcd(path[-1],num)!=1:
continue
path.append(num)
backroad(path)
path.pop()
if __name__=='__main__':
Length=[]
backroad([1],0)
print(Length)
6.时间显示#两个库分别可写
import time
n=int(input())
N=n/1000
print(time.strftime('%H:%M:%S',time.gmtime(N)))
import datetime
d=datetime.timedelta(seconds=N)
dd=datetime.timedelta(milliseconds=n)
ss=datetime.datetime(1970,1,1)+d
print(ss.strftime('%H:%M:%S'))
print(ss)
print(dd)
7.杨辉三角形#暴力求解
#杨辉三角形
Yang=[[1],[1,1]]
Ind=[1,1,1]
N=int(input())
while N not in Ind:
DD=[1]
for i in range(len(Yang[-1])-1):
DD.append(Yang[-1][i]+Yang[-1][i+1])
DD.append(1)
Ind+=DD
Yang.append(DD)
print(Yang)
print(Ind.index(N)+1)
8.左孩子右兄弟#递归
左孩子右兄弟
N_Tree={}#存储N叉树
N=int(input())
for i in range(2,N+1):
tem=int(input())
if tem not in N_Tree.keys():
N_Tree[tem]=[i]
else:
N_Tree[tem].append(i)
def Etree(t):#自顶向下递归,自底向上寻找
res=len(N_Tree[t])
TT = [0]
for i in N_Tree[t]:
if i in N_Tree.keys():#如果它为父节点,即它有孩子
TT.append(Etree(i))
res+=max(TT)
return res
print(N_Tree)
print(Etree(1))
9.异或数列#参考这个博主
#异或数列
import os
import sys
# 请在此输入您的代码
T = int(input())
for Y in range(1, T + 1):
a = []
sum = 0
ans = int(0)
mp = list(map(int, input().split()))
n = mp[0]
ma = int(0)
for j in range(1, len(mp)):
a.append(mp[j])
sum ^= mp[j]
ma = max(ma, mp[j])
x = 1
if sum == 0:
print(0)
continue
while x < ma:
x = x * 2
x = x * 2
while x > 0:
one = 0
zero = 0
for i in range(0, n):
if int(int(a[i] / x) % 2) == 1:
one += 1
else:
zero += 1
if (int(one % 2)) == 1:
if (int(zero & 1)) == 1 and one > 1:
ans = -1
else:
ans = 1
break
x = int(x / 2)
print(ans)
10.异或括号#我用的是组合的思想+回溯求解
#括号序列
def ComBack(SS,path,start):
global res
if sum(SS)==NN:
# print(SS)
res+=1
return
elif sum(SS)>NN or start>=len(AA):
return
for i in range(start,len(AA)):
if i not in path:
path.append(i)
for j in AA[i]:
SS.append(j)
ComBack(SS,path,i+1)
SS.pop()
path.pop()
if __name__=='__main__':
AA=[]
S=input()
NN=0
A,B='',''
if S.count('(')>S.count(')'):#缺少右括号,即用右括号插位置
A=S
B=')'*(S.count('(')-S.count(')'))
right=0
NN=len(B)
for i in range(len(A)-1):
if A[i]=='(' and A[i+1]=='(':
right += 1
if right>=NN:
right=NN
AA.append(list(range(1,right+1)))
elif A[i]==')' and A[i+1]=='(':
AA.append(list(range(1, right + 1)))
if A[0]==')' and right<NN:
right+=1
if right!=0:
AA.append(list(range(1, right + 1)))
elif S.count('(')<S.count(')'):#缺少左括号,即用左括号插位置
A = S
B = '(' * (S.count(')') - S.count('('))
left=0
NN=len(B)
for i in range(1,len(A)):
if A[-i]==')' and A[-i-1]==')':
left+=1
if left>=NN:
left=NN
AA.append(list(range(1, left + 1)))
elif A[-i]==')' and A[-i-1]=='(':
AA.append(list(range(1, left + 1)))
if A[0]==')' and left<NN:
left+=1
if left!=0:
AA.append(list(range(1, left + 1)))
#print(AA)
#print(B)
#print(NN)
# AA=[[1],[1,2],[1,2]]
res=0
for nn in range(len(AA)):
for num in AA[nn]:
ComBack([num],[],nn+1)
print(res)
结果: