Make Unreal REAL.
article thumbnail
Level 2. 124 나라의 숫자

 

 

십집수와 비교하며 패턴을 찾으면 된다.

 

124 나라에서는 십집수를 3으로 나눈 나머지가  0일 때 4, 4일 때 1, 2일 때 2가 된다.

 

124 나라에서 n자릿수로 바뀌는 십집수를 f(n)이라고 할 때,

f(0) = 0
f(n) = 3ⁿ⁻¹ + f(n - 1)

 

#include <iostream>
#include <algorithm>

using namespace std;

string solution(int n)
{
    string answer;

    while (n)
    {
        int mod = n % 3;
        (n /= 3) -= (mod == 0);

        answer += "412"[mod];
    }

    reverse(answer.begin(), answer.end());

    return answer;
}
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그