创建两个循环队列,一个循环用来存放十天干,一个循环列表存放十二地支,若天干队列为空,则让十天干入队列;若地支为空则让十地支依次入队列;若两个队都不为空,两个队列各取一个元素出列,并在屏幕上输出配对的天干地支;直到输出六十个甲子,程序结束
时间: 2023-05-28 17:06:50 浏览: 111
我是AI语音助手,以下是Python代码实现:
# 定义天干和地支的列表
tian_gan = ['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸']
di_zhi = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥']
# 定义队列长度
queue_length = 60
# 定义队列头和队列尾
tian_gan_front, tian_gan_rear = 0, 0
di_zhi_front, di_zhi_rear = 0, 0
# 定义计数器
count = 0
# 定义循环队列
tian_gan_queue = [None] * queue_length
di_zhi_queue = [None] * queue_length
while count < queue_length:
# 若天干队列为空,则让十天干入队列
if tian_gan_front == tian_gan_rear:
for i in range(len(tian_gan)):
tian_gan_queue[tian_gan_rear] = tian_gan[i]
tian_gan_rear = (tian_gan_rear + 1) % queue_length
# 若地支队列为空,则让十二地支依次入队列
if di_zhi_front == di_zhi_rear:
for i in range(len(di_zhi)):
di_zhi_queue[di_zhi_rear] = di_zhi[i]
di_zhi_rear = (di_zhi_rear + 1) % queue_length
# 若两个队列都不为空,两个队列各取一个元素出列,并在屏幕上输出配对的天干地支
if tian_gan_front != tian_gan_rear and di_zhi_front != di_zhi_rear:
tian_gan_element = tian_gan_queue[tian_gan_front]
tian_gan_front = (tian_gan_front + 1) % queue_length
di_zhi_element = di_zhi_queue[di_zhi_front]
di_zhi_front = (di_zhi_front + 1) % queue_length
print(tian_gan_element + di_zhi_element)
count += 1
阅读全文