BOJ : https://www.acmicpc.net/problem/3943

gitHub : https://github.com/junho0956/Algorithm/blob/master/3943/3943/%EC%86%8C%EC%8A%A4.cpp

 

요구하는 그대로 따라가주면 되는 문제.

단, iostream을 사용할 것이라면 테스트케이스가 10만개이므로

ios::sync_with_stdio(false); 을 사용해주도록 하자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdio>
#include <algorithm>
using namespace std;
#pragma warning(disable:4996)
 
int main() {
    int T;
    scanf("%d"&T);
 
    while (T--) {
        long long n;
        scanf("%lld"&n);
 
        long long ans = 1;
        while (n != 1) {
            ans = max(ans, n);
            if (n % 2) n = n * 3 + 1;
            else n /= 2;
        }
        printf("%lld\n", ans);
    }
 
    return 0;
}

 

 

복습) 20.02.08

+ Recent posts