当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢
随机推荐
Go编译原理系列8(变量捕获)
树莓派入门
国际原子能机构总干事警告称扎波罗热核电站安全形势已“完全失控”
蒲丰投针学习笔记
多行函数;group_by分组;having分组后筛选;单表查询总结
The use of DDR3 (Naive) in Xilinx VIVADO (3) simulation test
图像分割方法
中电金信技术实践|分布式事务简说
MySQL必知必会(初级篇)
如何用一条命令将网页转成电脑 App
知道创宇EDR系统实力通过中国信通院端点检测与响应产品能力评测
【地平线旭日X3派试用体验】从开机到点灯(第一节)
Leetcode刷题——二叉搜索树相关题目(98. 验证二叉搜索树、235. 二叉搜索树的最近公共祖先、1038. 从二叉搜索树到更大和树、538. 把二叉搜索树转换为累加树)
UMA & Hong Kong Polytechnic & Ali propose SP-ViT to learn 2D space prior knowledge for visual Transformer!
数据库对象-视图;存储过程
Flutter 使用 json_serializable 解析 JSON 支持泛型
微信服务号调用API实现微信报警
Hands-on Deep Learning_LeNet
A Survey of Multi-Label Classification under Supervised and Semi-Supervised Learning
将博客搬至CSDN