30个人准备过河,只有一条船,船只能坐15个人,0 15人,只能等待下一条船的到来,于是30个人排成一队排队的位置即为他们的编号,从一开始报数数到九的人出列上传,如此循环,直到最终只剩下15人为止,问都有哪些编号的人上传了呢?分别采用列表和字典储存数据来求解
这不是约瑟夫环吗?链表的模板题
列表模拟链表:
n = 30
nodes = list(range(1, n+2))
nodes[n] = 1
now = 1
while n > 15:
for _ in range(8):
prev, now = now, nodes[now]
print(now, end=" ")
nodes[prev] = nodes[now]
now = nodes[prev]
n -= 1
输出
9 18 27 6 16 26 7 19 30 12 24 8 22 5 23
#2 打开摄像头
video_capture=cv2.VideoCapture(0)
while True:
ret,frame=video_capture.read()
small_frame=cv2.resize(frame,(0,0),fx=0.25,fy=0.25) #将照片缩小为四分之一,便于处理
rgb_small_frame=small_frame[:,:,::-1] #按照倒序将cv2的bgr转换为rgb
face_locations=face_recognition.face_locations(rgb_small_frame)
face_encodings=face_recognition.face_encodings(rgb_small_frame,face_locations)
face_name_list=[]
for face_encoding in face_encodings:
matches=face_recognition.compare_faces(face_lib,face_encoding,tolerance=0.39)
name='未知人员'
if True in matches:
first_match_index=matches.index(True)
name=file_names[first_match_index][:-4]
face_name_list.append(name)
for (top,right,bottom,left),name in zip(face_locations,face_name_list):
#还原图片大小
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame,(left,top),(right,bottom),(0,0,255),2) #标注人脸信息
img_PIL=Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))
font=ImageFont.truetype('simhei.ttf',40)
draw=ImageDraw.Draw(img_PIL)
draw.text((left+6,bottom-6),name,font=font,fill=(255,255,255))
frame=cv2.cvtColor(np.asarray(img_PIL),cv2.COLOR_RGB2BGR)
save_recorder(name,frame)
cv2.imshow('window1',frame)
if cv2.waitKey(1) &0xFF==ord('q'): #1表示每一秒抓取一张照片用来识别
break
video_capture.release()