-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Silver V] Title: 분수찾기, Time: 32 ms, Memory: 31120 KB -BaekjoonHub
- Loading branch information
1 parent
d49acb4
commit e4d8df1
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# [Silver V] 분수찾기 - 1193 | ||
|
||
[문제 링크](https://www.acmicpc.net/problem/1193) | ||
|
||
### 성능 요약 | ||
|
||
메모리: 31120 KB, 시간: 32 ms | ||
|
||
### 분류 | ||
|
||
구현, 수학 | ||
|
||
### 제출 일자 | ||
|
||
2024년 9월 13일 12:51:10 | ||
|
||
### 문제 설명 | ||
|
||
<p>무한히 큰 배열에 다음과 같이 분수들이 적혀있다.</p> | ||
|
||
<table class="table table-bordered" style="width:30%"> | ||
<tbody> | ||
<tr> | ||
<td style="width:5%">1/1</td> | ||
<td style="width:5%">1/2</td> | ||
<td style="width:5%">1/3</td> | ||
<td style="width:5%">1/4</td> | ||
<td style="width:5%">1/5</td> | ||
<td style="width:5%">…</td> | ||
</tr> | ||
<tr> | ||
<td>2/1</td> | ||
<td>2/2</td> | ||
<td>2/3</td> | ||
<td>2/4</td> | ||
<td>…</td> | ||
<td>…</td> | ||
</tr> | ||
<tr> | ||
<td>3/1</td> | ||
<td>3/2</td> | ||
<td>3/3</td> | ||
<td>…</td> | ||
<td>…</td> | ||
<td>…</td> | ||
</tr> | ||
<tr> | ||
<td>4/1</td> | ||
<td>4/2</td> | ||
<td>…</td> | ||
<td>…</td> | ||
<td>…</td> | ||
<td>…</td> | ||
</tr> | ||
<tr> | ||
<td>5/1</td> | ||
<td>…</td> | ||
<td>…</td> | ||
<td>…</td> | ||
<td>…</td> | ||
<td>…</td> | ||
</tr> | ||
<tr> | ||
<td>…</td> | ||
<td>…</td> | ||
<td>…</td> | ||
<td>…</td> | ||
<td>…</td> | ||
<td>…</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
|
||
<p>이와 같이 나열된 분수들을 1/1 → 1/2 → 2/1 → 3/1 → 2/2 → … 과 같은 지그재그 순서로 차례대로 1번, 2번, 3번, 4번, 5번, … 분수라고 하자.</p> | ||
|
||
<p>X가 주어졌을 때, X번째 분수를 구하는 프로그램을 작성하시오.</p> | ||
|
||
### 입력 | ||
|
||
<p>첫째 줄에 X(1 ≤ X ≤ 10,000,000)가 주어진다.</p> | ||
|
||
### 출력 | ||
|
||
<p>첫째 줄에 분수를 출력한다.</p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
[풀이] | ||
- 대각선 i번째 구하기 | ||
- i 홀수? 왼쪽 끝부터 (i/1) > (i--/1++) distance 만큼 | ||
- i 짝수? 오른쪽 끝부터 (1/i) > (1++/i--) distance 만큼 | ||
""" | ||
|
||
import sys | ||
|
||
readline = lambda: sys.stdin.readline().strip() | ||
readline = input | ||
|
||
target_num = int(readline()) | ||
|
||
# get (좌 or 우) 끝 번호 | ||
cross_idx = 1 | ||
end_num = 1 | ||
while end_num < target_num: | ||
cross_idx += 1 | ||
end_num += cross_idx | ||
|
||
# 거리만큼 이동 후 분수 구하기 | ||
distance = end_num - target_num | ||
if cross_idx % 2 == 0: | ||
denominator = cross_idx | ||
numerator = 1 | ||
|
||
while distance: | ||
distance -= 1 | ||
denominator -= 1 | ||
numerator += 1 | ||
else: | ||
denominator = 1 | ||
numerator = cross_idx | ||
|
||
# distance 만큼 이동해서 분수 구하기 | ||
distance = end_num - target_num | ||
while distance: | ||
distance -= 1 | ||
denominator += 1 | ||
numerator -= 1 | ||
|
||
|
||
print(f"{denominator}/{numerator}") |