Lazy loaded image
技术分享
😵<每日一题>洛谷-P1563 [NOIP2016 提高组] 玩具谜题
字数 420阅读时长≈ 2 分钟
2024-10-13
2024-12-24
1
type
status
date
slug
summary
tags
category
icon
password
😀
24/10/12 慢慢来,给自己时间;慢慢来,让自己理解;慢慢来,让自己坚持

简单的模拟问题

题目🔗
 
思路:有向外面和向里面,还有左右,看起来很复杂(其实也不会),我们只需要用一个函数来判断当前是要向那一边走(负数表示一个方向,正数表示一个方向);线性结构变成圈我们可以通过取模运算来实现列表的闭合;然后通过字典和列表的配合,算出向左还是向右多少步,就可以了

取模妙用:

  • (i + size) % size 就可以实现在size这个长度里面循环
 

代码⬇️

'''P1563 [NOIP2016 提高组] 玩具谜题''' def outOrIn(direction: int,head: int) -> bool: # 总合向左就返回True if direction == 0 and head == 0: return True elif direction == 0 and head == 1: return False elif direction == 1 and head == 0: return False elif direction == 1 and head == 1: return True # n行m列 n ,m = input().split() n = int(n) m = int(m) set = 0 # 要走的步长和方向 people = {} # 空字典 persons = list() # 存人物圈 for i in range(n): d, job = input().split() people[job] = int(d) persons.append(job) # 创建字典,名字对应0或1 for i in range(m): head, nums = input().split() ''' 精华 (set + len(persons)) % len(persons) 收尾链接,形成闭环 ''' if outOrIn(people[persons[(set + len(persons)) % len(persons)]],int(head)): set -= int(nums) else: set += int(nums) print(persons[(set + len(persons)) % len(persons)]) # AC!

🤗 总结归纳


继续加油吧!少年!
 
💡
欢迎您在底部评论区留言,一起交流~
上一篇
<每日AC>-洛谷P2251 质量检测
下一篇
正则表达式

评论
Loading...
logo