: https://cses.fi/problemset/task/1068
문제 설명
Consider an algorithm that takes as input a positive integer n. If n is even, the algorithm divides it by two, and if n is odd, the algorithm multiplies it by three and adds one. The algorithm repeats this, until n is one. For example, the sequence for n=3 is as follows:
3→10→5→16→8→4→2→1
입력
The only input line contains an integer n.
출력
Print a line that contains all values of n during the algorithm.
입력 예
3
출력 예
3 10 5 16 8 4 2 1
문제 풀이
입력받는 정수값이 너무 크면, 프로그램을 종료하도록 하고, 문제에서 설명한 알고리즘을 구현했다.
프로그램 설명
더보기
int main()
{
int n;
cin >> n;
if (n > 1000000) return 0;
while(true)
cout << n << " " ;
if ( n == 1 ) break;
else if ( n %2 == 0) n /= 2;
else n = n*3 + 1;
Introductory problems ( link )
'CSES' 카테고리의 다른 글
CSES 1. Permutations (1070) (0) | 2019.09.19 |
---|---|
CSES 1. Increasing Array (1094) (0) | 2019.09.19 |
CSES 1. Repetitions (1069) (0) | 2019.09.16 |
CSES 1. Missing Number (1083) (0) | 2019.09.15 |
CSES - Introductory Problems (0) | 2019.09.14 |