一、自行车停放
题目
思路
- list模拟
- 链表模拟
代码
list模拟
import time
n = int(input())
a = []
a.append(int(input()))
##t1 = time.perf_counter()
for i in range(n-1):
x,y,z = list(map(int,input().split()))
y_index = a.index(y)
if z == 0:
a.insert(y_index,x)
else:
a.insert(y_index+1,x)
for i in range(len(a)):
print(a[i],end = " ")
##t2 = time.perf_counter()
##print(f"UsedTime:{t2-t1}")
链表模拟
import time
class Node:
def __init__(self,value):
self.value = value
self.next = None
class linknode:
def __init__(self,node):
self.head = Node(None)
self.head.next = node
def leftinsert(self,loc,value):
a = Node(value)
b = self.head
while b.next.value != loc:
b = b.next
a.next = b.next
b.next = a
def rightinsert(self,loc,value):
a = Node(value)
b = self.head
while b.value != loc:
b = b.next
a.next = b.next
b.next = a
def show(self):
t = self.head.next
while t != None:
print(t.value,end = " ")
t = t.next
print("")
n = int(input())
f = int(input())
##t1 = time.perf_counter()
a = Node(f)
b = linknode(a)
for i in range(n-1):
x,y,z = list(map(int,input().split()))
if z == 0:
b.leftinsert(y,x)
else:
b.rightinsert(y,x)
b.show()
##t2 = time.perf_counter()
##print(f"UsedTime:{t2-t1}")
二、约瑟夫环
题目
思路
使用list模拟或者使用循环链表实现
代码
list模拟
n,k,m = list(map(int,input().split()))
a = [i+1 for i in range(n)]
s = k-1
while(len(a) != 0):
s = (s+m-1)%len(a)
print(a.pop(s))
收获&感想:
- 还是不够熟练
- 有点粗心、脑子转不过来
- 进度有点慢了