본문 바로가기
CodeSignal/Arcade

CodeSignal [30/60] circleOfNumbers

by 벤진[Benzene] 2020. 5. 21.

문제: Consider integer numbers from 0 to n - 1 written down along the circle in such a way that the distance between any two neighboring numbers is equal (note that 0 and n - 1 are neighboring, too).

Given n and firstNumber, find the number which is written in the radially opposite position to firstNumber.

 

Example:

Example

For n = 10 and firstNumber = 2, the output should be
circleOfNumbers(n, firstNumber) = 7.

 

-문제해결 정리-

1. 수학적으로 계산하면 원의 반대편에 있는 점은 다음과 같다.

firstNumber가 n // 2 보다 작을경우:

반대편에 있는 점 --> (firstNumber) + (n // 2)

 

firstNumber가 n // 2 보다 클 경우:

반대편에 있는 점 --> (firstNumber) - (n // 2)

 

2. 이 수식을 그대로 코드에 구현

 

 

모든 문제에 대한 저작권은 CodeSignal 회사에 있습니다. [Copyright © 2020 BrainFights Inc. All rights reserved]

https://codesignal.com/

댓글