728x90
문제
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. If N is even, the number N is divided by 2. Each time the number is multiplied or divided, the score increases by one point. The game ends -- and the score is finalized -- when N becomes 1. If N is initially 1, the score is 0.
Here's an example with N starting at 5:
The final score is 5.
입력
Line 1: A single integer, N
출력
Line 1: A single integer that is the score for this game when starting at N
풀이
N이 1이 될 때까지 홀수면 3배를 해주고 1을 더하고, 짝수면 2로 나누어주면서 연산 횟수를 세주자.
소스코드
출처
728x90
'PS > Baekjoon Online Judge' 카테고리의 다른 글
[백준 13939] Imena [Java] (0) | 2024.07.18 |
---|---|
[백준 02579] 계단 오르기 [C/C++] (0) | 2024.07.18 |
[백준 09095] 1, 2, 3 더하기 [C/C++] (0) | 2024.07.17 |
[백준 06162] Superlatives [Python] (0) | 2024.07.17 |
[백준 01463] 1로 만들기 [C/C++] (0) | 2024.07.15 |