当前位置:网站首页>LeetCode每日一题(858. Mirror Reflection)
LeetCode每日一题(858. Mirror Reflection)
2022-08-04 11:56: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
把房间想象成一个电梯, 电梯的所有 m * p(m 为大于 0 的整数)的高度处左右两边都挂着传感器, 奇数层挂 2 和 1, 偶数层挂 2 和 0,然后这个题就变成了求最小公倍数的题
impl Solution {
pub fn mirror_reflection(p: i32, q: i32) -> i32 {
let mut m = 1;
let mut is_up = true;
loop {
if p * m % q == 0 {
if p * m / q % 2 == 0 {
return 2;
}
if is_up {
return 1;
}
return 0;
}
m += 1;
is_up = !is_up;
}
}
}
边栏推荐
猜你喜欢

MOSFET米勒平台(Miller Plateau)

Go编译原理系列8(变量捕获)

博云入选 Gartner 中国 DevOps 代表厂商

God Space - the world's first Web3.0-based art agreement creative platform, broadening the boundaries of multi-art integration

OAuth2图文快速入门

HyperLynx仿真(一)LineSim简单介绍

Nacos手摸手教学【二】Nacos注册中心

数据库对象-视图;存储过程

244 page PDF!"2022 China cloud computing ecological blue book published

【目标检测】yolov2特征提取网络------Darknet19结构解析及tensorflow和pytorch实现
随机推荐
电源测试之输出动态响应(Output Dynamic Response Test)
AI 助力双碳目标:让每一度电都是我们优化的
博云入选 Gartner 中国 DevOps 代表厂商
Zhihu Data Analysis Training Camp
The use of DDR3 (Naive) in Xilinx VIVADO (1) to create an IP core
如何过一个充满科技感的七夕?华为告诉你
Disc burning steps
剑指长城炮? 长安全新皮卡官方谍照
揭秘交换机市场内幕,“准工业级”猫腻你中招了吗?
[Flight Control Development Advanced Course 7] Crazy Shell Open Source Formation UAV - Formation Flight
Leetcode刷题——路径总和
IBM Q复制ALTER ADD COLUMN
使用json-server快速搭建本地数据接口
节流函数(每隔一段时间就会执行一次)
Leetcode Brush Questions - Path Sum
中电金信技术实践|分布式事务简说
WPF 截图控件之画笔(八)「仿微信」
【RISC-V】Trap和Exception
技术分享| 融合调度系统中的电子围栏功能说明
【VBox】解决复制VBox虚拟机后提示硬盘UUID 已经存在的问题