190128 백준 알고리즘 문제풀기*
28 Jan 2019 | baekjoon[1011] Fly me to Alpha Centauri*
문제
-
두 수와 case 횟수가 주어질 때, 이전 양쪽 숫자와의차이가 1 또는 0이 되도록 하여 가장 짧은 거리로 두 수를 주파 할 수 있는 횟수를 출력
- 수 들을 나열 할 때, 대칭이 되는 수 n을 찾고, 그 n에 따라 주파 거리가 결정 됨.
- [1], [1, 2, 1], [1, 2, 3, 2, 1], … , [1, 2, …, n-1, n, n-1, …, 2, 1] 의 관계를 보이는 수 n을 찾는다.
- 그 n에 따라, $n^{2}$ 보다 작거나 같으면 $n\times 2-1$번, 크면 $n\times 2$번의 jump를 한다.
정답
def findjump(dist):
minN = powN = maxN = jumpCnt = 0
n = 1
while(1):
powN = n*n
minN = powN - n + 1
maxN = powN + n
if minN<=dist and dist<=maxN:
if minN<=dist and dist<=powN:
jumpCnt = (n<<1) - 1
else:
jumpCnt = n<<1
break
n += 1
return jumpCnt
for _ in range(int(input())):
nums = input().split()
dist = int(nums[1]) - int(nums[0])
print(findjump(dist))
[10250] ACM 호텔
문제
- 입력으로 case 수, h, w, 손님수가 주어질 때, 손님이 몇 번 방에 묵는지를 계산
- 손님은 층수가 낮을수록, 엘레베이터에 가까울수록 해당 방을 선호함
정답
for _ in range(int(input())):
nums = input().split()
height = int(nums[0])
width = int(nums[1])
num = int(nums[2])
floor = str(num % height)
th = str(int(num / height) + 1)
if floor=='0':
floor = str(height)
th = str(int(num/height))
if len(th)<2:
th = '0'+th
result = floor+th
print(result)