[Python] 백준 1158번 : 요세푸스 문제
2025. 4. 9. 00:56
접근 방법 1
while문을 활용하여 문제 해결 시도 -> 시간 초과 에러 발생
def josephus_problem(n, k):
numbers = [False] * n
cur_index = 0
answer = []
while len(answer) < n:
count = 0
while count < k:
if not numbers[cur_index]:
count += 1
if count == k:
break
cur_index = (cur_index + 1) % n
numbers[cur_index] = True
answer.append(cur_index + 1)
return answer
n, k = map(int, input().split())
answer = josephus_problem(n, k)
print(f"<{', '.join(map(str, answer))}>")
접근 방법 2
리스트 슬라이싱을 활용하여 문제 해결 시도
'''
해결 중
'''
'코딩테스트 > BOJ' 카테고리의 다른 글
[Python] 백준 2358번 : 평행선 (0) | 2025.04.11 |
---|---|
[Python] 백준 3986번 : 좋은 단어 (0) | 2025.04.08 |
[Python] 백준 31458번 : !!초콜릿 중독 주의!! (0) | 2025.04.02 |
[Python] 백준 10820번 : 문자열 분석 (0) | 2025.04.01 |
[Python] 백준 1032번 : 명령 프롬프트 (0) | 2025.03.31 |