Mirror Reflection (M)
題目
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
題意
四面墻裝有鏡子,其中三個角裝有傳感器,從剩余的那個角打出一束激光,要求判斷第一個接收到激光的傳感器,
思路
將激光按照可以穿透鏡面進行處理,參考官方解答及[LeetCode] 858. Mirror Reflection 鏡面反射,
代碼實作
Java
class Solution {
public int mirrorReflection(int p, int q) {
int gcd = gcd(p, q);
p /= gcd;
q /= gcd;
return q % 2 == 0 ? 0 : p % 2 == 0 ? 2 : 1;
}
private int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/222688.html
標籤:其他
