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
'Algorithm > PS' 카테고리의 다른 글
[BOJ/백준] 6321 - IBM 빼기 1 [python] (0) | 2023.03.17 |
---|---|
[BOJ/백준] 6249 - TV Reports [python] (0) | 2023.03.17 |
[BOJ/백준] 6162 - Superlatives [python] (0) | 2023.03.16 |
[BOJ/백준] 6131 - 완전 제곱수 [python] (0) | 2023.03.16 |
[BOJ/백준] 6030 - Scavenger Hunt [python] (0) | 2023.03.16 |