当前位置:网站首页>LeetCode Daily Question (858. Mirror Reflection)
LeetCode Daily Question (858. Mirror Reflection)
2022-08-04 12:13:00 【wangjun861205】
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.
Given the two integers p and q, 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.
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.
Example 2:
Input: p = 3, q = 1
Output: 1
Constraints:
- 1 <= q <= p <= 1000
Imagine the room as an elevator, all the heights of m * p (m is an integer greater than 0) of the elevator are hung with sensors on the left and right sides, 2 and 1 on the odd floor, 2 and 0 on the even floor, and then thisThe problem becomes the problem of finding the least common multiple
impl Solution {pub fn mirror_reflection(p: i32,span> q: i32) -> i32 {let mut m = 1;let mut is_up = true;loop {if p * m % q == 0 {if p * m / q % 2 == 0span> {return 2;}if is_up {return 1;}return 0;}m += 1;is_up = !is_up;}}}
边栏推荐
猜你喜欢
随机推荐
独立站卖家如何使用 WhatsApp Business API 建立有意义的客户关系?
开发小程序插件如何实现盈利?
小程序实战(一)- 骨架屏的应用与实现
云原生Devops 的实现方法
技术分享| 融合调度系统中的电子围栏功能说明
简要介绍电源效率测试
244 page PDF!"2022 China cloud computing ecological blue book published
【目标检测】yolov3特征提取网络------Darknet53网络及pytorch实现
Rust 从入门到精通04-变量
电源输出的Overshoot和Undershoot 测试
Yolov5 测试和训练自己的数据集
opencv------图片转化为视频
Leetcode Brush Questions - Path Sum
yolov5——detect.py代码【注释、详解、使用教程】
表的完整性约束;非外键约束
POJ1094Sorting It All Out题解
喂,你知道节流是什么吗?
使用Stream多年,collect还有这些“骚操作”?
C#控制台退出前操作
IBM Q复制新增QSUB