QA & Engineering Blog

A Blog about Quality · Automation · Engineering

🏠 홈으로

[Bronze I] 분수찾기 - 1193

문제 링크

성능 요약

메모리: 30616 KB, 시간: 36 ms

분류

구현(implementation), 수학(math)

문제 설명

무한히 큰 배열에 다음과 같이 분수들이 적혀있다.

1/1 1/2 1/3 1/4 1/5
2/1 2/2 2/3 2/4
3/1 3/2 3/3
4/1 4/2
5/1

이와 같이 나열된 분수들을 1/1 → 1/2 → 2/1 → 3/1 → 2/2 → … 과 같은 지그재그 순서로 차례대로 1번, 2번, 3번, 4번, 5번, … 분수라고 하자.

X가 주어졌을 때, X번째 분수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 X(1 ≤ X ≤ 10,000,000)가 주어진다.

출력

첫째 줄에 분수를 출력한다.

💡 Solutions

📄 분수찾기.py

target = int(input())

n = 0
cnt = 0
while n < target:
    n += cnt
    cnt += 1
depth = cnt - 1
is_n_0 = True if depth % 2 else False
pref = sum([i for i in range(1,depth)])
to_walk = target - pref - 1 
div = f'{depth - to_walk}/{to_walk + 1}' if is_n_0 else f'{to_walk + 1}/{depth - to_walk}'
print(div)