Lintcode1511 · Mirror Reflection go

本文探讨了一种特殊的方形房间,其四面墙壁都装有镜子。从西南角出发的激光束首次碰到东墙时距离0号接收器为q。算法问题要求确定激光束最终会碰到哪个接收器。给定房间边长p和初始位置q,通过计算判断激光束的反射路径。题目提供了解决方案,并附有示例解释了不同输入情况下激光束的反射路径。该问题涉及镜面反射和数学计算,是算法和逻辑思维的结合。
摘要由CSDN通过智能技术生成

/**
1511 · Mirror Reflection
Algorithms
Medium
Accepted Rate
74%

DescriptionSolutionNotesDiscussLeaderboard
Description
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.)

1 <= p <= 10001<=p<=1000
0 <= q <= p0<=q<=p
Example
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.
https://lintcode-media.oss-us-west-1.aliyuncs.com/problem/reflection.png
Example 2:

Input: p = 3, q = 1
Output: 1
Explanation: The ray meets receptor 1 the first time it gets reflected back to the left wall.
Tags
Company
Google
Recommend Courses

https://www.cnblogs.com/grandyang/p/10646040.html

https://www.lintcode.com/problem/1511/
*/

/**
 * @param p: an integer
 * @param q: an integer
 * @return: the number of the receptor that the ray meets first
 */
func mirrorReflection (p int, q int) int {
	// Write your code here
	for {
		if !(p % 2 == 0 && q % 2 ==0) {
			break
		}
		p /=2
		q /= 2
	}
	if p % 2 == 0 {
		return 2
	}
	if q % 2 == 0 {
		return 0
	}
	return 1
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值