[백준/파이썬] 13236번 풀이

업데이트:



문제 정보


풀이

문제

The Collatz conjecture is a conjecture in mathematics named after Lothar Collatz, who first proposed it in 1937 and is still an open problem in mathematics.  The sequence of numbers involved is referred to as the hailstone sequence or hailstone numbers (because the values are usually subject to multiple descents and ascents like hailstones in a cloud. The conjecture is that no matter what number you start with, you will always eventually reach 1. …

입력 요약
A integer value n. The value of n will be between 1 and 100000.

출력 요약
Print the Collatz sequence with the elements separated by a single space. Do not add any space at the beginning or the end of the line.

코드

l=[int(input())]
while l[-1]!=1:
    if l[-1]%2==0:l.append(l[-1]//2)
    else:l.append(l[-1]*3+1)
print(*l)

설명

핵심은 구현 관점에서 The Collatz conjecture is a conjecture in mathematics named after Lothar Collatz, who first proposed it in 1937 and is still an open problem in mathem …를 만족하도록 로직을 구성하는 것입니다.

코드는 입력을 파싱한 뒤 조건 분기와 계산을 순서대로 수행하고, 문제에서 요구한 형식으로 결과를 출력합니다.

경계값과 예외 케이스도 함께 고려해 오답이 나기 쉬운 상황을 방지합니다.



다음 읽을거리

관련 허브 페이지에서 같은 주제의 글을 이어서 확인할 수 있습니다.

댓글남기기