当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢

傅里叶级数与傅里叶变换学习

深度学习------pytorch-gpu环境搭建

蒲丰投针学习笔记

Flutter使用 json_serializable 解析 JSON 最佳方案

The use of DDR3 (Naive) in Xilinx VIVADO (1) to create an IP core

Leetcode Brush Questions - Path Sum

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

企业应当实施的5个云安全管理策略

MOSFET米勒平台(Miller Plateau)

图像分割方法
随机推荐
你知道吗?那些专属于代码的浪漫~
opencv------图片转化为视频
cat /proc/kallsyms found that the kernel symbol table values are all 0
Rust 从入门到精通04-变量
[Flight Control Development Advanced Course 7] Crazy Shell Open Source Formation UAV - Formation Flight
六石编程学:编程中的直线思维与自然思维
剑指offer专项突击版第19天
从零开始Blazor Server(7)--使用Furion权限验证
使用Stream多年,collect还有这些“骚操作”?
光盘刻录步骤
ESP8266-Arduino编程实例-APDS-9930环境光和趋近感器驱动
Implementation principle of function emplace_back in vector
244页pdf!《2022 中国云计算生态蓝皮书》发布
Xilinx VIVADO 中 DDR3(Naive)的使用(1)创建 IP 核
终于有人把分布式机器学习讲明白了
SchedulX V1.5.0发布,提供快速压测、对象存储等全新功能!
Tapdata 开源项目基础教程:功能特性及实操演示
到底什么是JS原型
OAuth2图文快速入门
小程序实战(三) - head组件的封装与使用