Question
There is a special square room with mirrors on each of the four walls. Except for the southwest corner, there are receptors on each of the remaining corners, numbered
0
,1
, and2
.The square room has walls of length
p
and a laser ray from the southwest corner first meets the east wall at a distanceq
from the0<sup>th</sup>
receptor.Given the two integers
p
andq
, return the number of the receptor that the ray meets first.The test cases are guaranteed so that the ray will meet a receptor eventually.
Solution
激光在箱子里折射,可以想象成在无限延展的空间内直线发射光线。
因此三个接收器可以被视作组成了一个无限延展的矩阵。
只需要将q和p化简,然后确定坐标(q, p)所对应的接收器即可。
由于接收器是间隔的,因此只需要先化简q,p后计算两者的奇偶性即可。
如果q与p皆为奇数,则返回1号接收器。
如果q是奇数,p不是,则返回0号接收器。
如果p是奇数,q不是,则返回2号接收器。
Code
1 | class Solution { |