본문 바로가기
python-algorithm

[백준] 3943

by 무적김두칠 2021. 2. 26.

1
2
3
4
5
6
7
8
9
10
import sys
for _ in range(int(sys.stdin.readline())):
    tmp=[]
    n=int(sys.stdin.readline())
    while True:
        if 1 in tmp: break
        tmp.append(n)
        if n%2==0 : n//=2
        else: n=n*3+1
    print(max(tmp))
cs

일단 문제 자체는 쉬운데 문제는 시간제한이 걸려서 pypy3 로 통과했습니다.
손으로 그려가면서 해보니 n이 2의 제곱수가 되면 반복문을 탈출하는 것으로 하면 아마 시간이 조금 더 단축 될 것 같습니다.

반응형

'python-algorithm' 카테고리의 다른 글

[백준] 4328  (0) 2021.02.26
[백준] 4299  (0) 2021.02.26
[백준] 2997  (0) 2021.02.26
[백준] 2765  (0) 2021.02.25
[백준] 2740  (0) 2021.02.23

댓글