858. Mirror Reflection
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, and 2.
The square room has walls of length p, and a laser ray from the southwest corner first meets the east wall at a distance q from the 0th receptor.
Return the number of the receptor that the ray meets first. (It is guaranteed that the ray will meet a receptor eventually.)
Example 1:
Input: p = 2, q = 1
Output: 2
Explanation: The ray meets receptor 2 the first time it gets reflected back to the left wall.
Note:
1 <= p <= 10000 <= q <= p
class Solution { public int mirrorReflection(int p, int q) { int m = 1, n = 1; while(p * m != q * n) { n++; m = q * n / p; } if(n % 2 == 0) return 2; if(n % 2 == 1 && m % 2 == 1) return 1; if(n % 2 == 1 && m % 2 == 0) return 0; return -1; } }
服了,什么数学题。
要是能折射到旮旯的话有要求mp = nq。
首先如果折射odd time, n % 2 == 0。说明肯定反射到左边了, 返回2
否则是右边,可能是1或2。这个时候看房子增加了多少,如果是偶数次(包括0),这时m%2 == 1说明反射到1了.
否则就是0.


浙公网安备 33010602011771号