Algorithm/PS

[BOJ/백준] 6190 - Another Cow Number Game [python]

chanwoong1 2023. 3. 16. 20:05
728x90

문제 링크

 

6190번: Another Cow Number Game

The cows are playing a silly number game again. Bessie is tired of losing and wants you to help her cheat. In this game, a cow supplies a number N (1 <= N <= 1,000,000). This is move 0. If N is odd, then the number N is multiplied by 3 and incremented by 1

www.acmicpc.net

문제 풀이

입력값을 규칙에 따라 1이 될 때까지 몇 번 반복되는지 출력하면 된다.
n이 주어진다면 규칙은 다음과 같다.

  • 홀수일 경우, 다음 n은 3 * n + 1이다.
  • 짝수일 경우, 다음 n은 n / 2이다.

정답 코드

cnt = 0
N = int(input())
while N > 1 :
    if N % 2 == 0 : N //= 2
    else : N = 3 * N + 1
    cnt += 1
print(cnt)
728x90