洛谷线性表训练python代码
栈
线性表主要是栈和队列的一些使用,这两个数据结构主要用于先进先出和先进后出两中
后缀表达式:
stack =[]
string1 = input()
temp = 0
for i in range(len(string1)):
if(string1[i]=='@'):
break
elif(string1[i]=='.'):
stack.append(temp)
temp = 0
elif(string1[i] >= '0' and string1[i]<='9'): #
temp=(10*temp+int(string1[i])-int('0')) #有三个
elif(string1[i]=='+'):
x = stack.pop()
y = stack.pop()
stack.append(x+y)
elif(string1[i]=='*'):
x = stack.pop()
y = stack.pop()
stack.append(x*y)
elif(string1[i]=='-'):
x = stack.pop()
y = stack.pop()
stack.append(y-x) #也
elif(string1[i]=='/'):
x = stack.pop()
y = stack.pop()
stack.append(y//x) #当时用
print(int(stack.pop()))
循环队列
在python中的循环队列
import queue
queue = queue.Queue()
n,m = map(int,input().split()) #
for i in range(1,n+1):
queue.put(i)
temp=0
while(not queue.empty()):
temp+=1
if(temp!=m):
value = queue.get()
queue.put(value)
else:
value = queue.get()
print(value,end=" ")
temp=0