第一题(队列):
![](https://i-blog.csdnimg.cn/blog_migrate/25b26fe660e2e5c9d9c22c837c51c9aa.png)
import os
import sys
# 请在此输入您的代码
n = eval(input())
deque=[]
for i in range(n):
s=list(map(int, input().split()))
if s[0]==1:
# 入队
deque.append(s[1])
elif s[0]==2:
# 出队
if(len(deque)>0):
print(deque[0])
del deque[0]
else:
print('no')
break
elif s[0]==3:
print(len(deque))
方法:
![](https://i-blog.csdnimg.cn/blog_migrate/89f98b4305084b2afded6b48bf922877.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d1a39a135d306f54b707ab7360a70581.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ec08f44ebe02da63264328818d0f4cff.png)
第二题(栈)
![](https://i-blog.csdnimg.cn/blog_migrate/077b16a9ef8861d2bdd0c8dd02b763ea.png)
import os
import sys
#(源地址,借助地址,目的地址)
# 请在此输入您的代码
st =[[ 0 for i in range(30000)] for i in range(4)]
sum = 0
def move(x, y,n):
global sum
element = st[x].pop()
st[y].append(element)
sum +=1
a, b ='',''
if x==1 : a='A'
if x==2: a='B'
if x==3: a='C'
if y==1: b='A'
if y==2: b='B'
if y==3: b='C'
if sum == m:
print('#', n,': ',a,"->",b, sep="")
def hanoi(n,x,y, z):
if (n == 1): move(x, z, n)
else:
hanoi(n-1,x,z,y)
move(x,z, n)
hanoi(n-1,y,x,z)
n,m = map (int,input().split())
for i in range(n) : st[1].append(i)
hanoi(n,1,2,3)
print(sum)
建议使用deque实现队列和栈(也可list)
第三题(二叉树)
![](https://i-blog.csdnimg.cn/blog_migrate/9515d0b504771473fd9fb51a7e6b06e2.png)
import os
import sys
# 请在此输入您的代码
from math import *
n= int(input())
c = int(log(n,2))+1 # 计算有多少层
a = [0]+list(map(int,input().split())) # 转为下标为一开始
s = [0] * (c+1) # 初始化,记录每层权重
for i in range(1,c+1): #有c层
s[i] = sum(a[2**(i-1):2**i-1 +1]) # 切片最后一个取不到
print(s.index(max(s)))
![](https://i-blog.csdnimg.cn/blog_migrate/effcb028f8347d8c4b692d0236f9953b.png)
import os
import sys
# 请在此输入您的代码
def build(p,L,R):
if L==R: # 叶子结点
if s[R] =='1':
tree[p]='I'
else:
tree[p]='B'
return #返回
mid = (L+R)//2
build(2*p,L,mid)
build(2*p+1,mid+1,R)
if tree[2*p]=='B' and tree[2*p+1]=='B':
tree[p]=='B'
elif tree[2*p]=='I' and tree[2*p+1]=='I':
tree[p]=='I'
else:
tree[p]='F'
def postorder(p):
if tree[2*p] !='':
postorder(2*p)
if tree[2*p+1] !='':
postorder(2*p+1)
print(tree[p],end='')
n=int(input())
s=[0]+list(input()) # s[1]
tree =['']*4400
build(1,1,len(s)-1)
postorder(1) #root=1
第四题
![](https://i-blog.csdnimg.cn/blog_migrate/5505c41aab3a4e6df2fe9d176f707da6.png)
import os
import sys
# 请在此输入您的代码
n = int(input())
mylist=[0]+list(map(int,input().split()))
remember=[]
for i in range(1,n):
remember.append(mylist[i+1]-mylist[i])
print(max(remember))
第五题
![](https://i-blog.csdnimg.cn/blog_migrate/f74c151c232b5906739cc0eface8a54d.png)
import os
import sys
# 请在此输入您的代码
n , m =map(int,input().split())
marth = [[int(x) for x in input().split()] for _ in range(n)] # 记录矩阵
def search (i,j):
if(marth[i][j]):
return 9
cnt =0
for x in range(i-1,i+2):
for y in range(j-1,j+2):
if 0<=x<n and 0<=y<m: # 相当于处理了边界不足9个的情况 (n X m) 注意 x,y的取值范围
cnt+=marth[x][y]
return cnt
map_ = [[search(i,j) for j in range(m)] for i in range(n)] # 深拷贝方法,强烈推荐通过这个创建二维数组
for i in map_:
print(*i)
第六题
![](https://i-blog.csdnimg.cn/blog_migrate/534603b9925cb1628d57ffa1527474e4.png)
sky = ['jia','yi','bing','ding','wu','ji','geng','xin','ren','gui']
earth = ['zi','chou','yin','mao','chen','si','wu','wei','shen','you','xu','hai']
years = [sky[i % 10] + earth[i % 12] for i in range(60)] # 以60为周期
name, year = 'gengzi', 2020
index = years.index(name) #获得在列表的位置
input_year = int(input())
delt = input_year - year # 计算与已知位置的差
print(years[(index + delt) % 60]) # 加上索引然后取余
第七题
![](https://i-blog.csdnimg.cn/blog_migrate/897b1cf3c3a062a2f2cef7b5add20f05.png)
import os
import sys
# 请在此输入您的代码
n = int(input())
m =[0]+ list(map(int,input().split()))
ans =0
#方法一 通过一半
# for i in range(1,n):
# ans+=m[i]*sum(m[i+1:n+1]) # a1*(a2+a3...an) an-1 * an
# print(ans)
# 方法二 通过全部
sum = [0]*(n+1)
for i in range(1,n): # sum[1] - sum [n-1]
sum[i]=sum[i-1]+m[i]
for i in range(1,n):
ans += sum[i] * m[i+1]
print(ans)
# 通过.3 暴力法
第八题
![](https://i-blog.csdnimg.cn/blog_migrate/4e498d2cfc2f6ef3599ad87deed393ab.png)
import os
import sys
# 请在此输入您的代码
mydict ={}
n = int(input())
for i in range(n):
x = int(input())
if x in mydict.keys():
mydict[x]+=1
else:
mydict[x]=1
#mydict[x]=mydict.get(x,0)+1 写法二
key = list(mydict.keys())
key.sort()
for i in key:
print(i,mydict[i])