콘솔창 & 윈도우창/코딩 테스트

[실버3] 삼삼한 수 2

게임 개발 2024. 10. 18. 15:30

 

3의 0제곱도 포함하는 것이니, n%3의 값이 1이 될 수 있다.

또한 음의 수는 포함하지 않으나, "작거나" 같은 수라 했다.

작거나는 0의 예외처리를 뜻하는 것이다.

0의 경우 예외처리가 바로 생각나지 않아서 4번 틀렸다.

 

 

#include <string>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>

using namespace std;

bool IsSumOfPowers(long long int n);

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    long long int n;

    cin >> n;

    if (n == 0) {
        printf("NO"); 
        return 0;
    }

    if(IsSumOfPowers(n)) {
        printf("YES");
    }
    else
    {
        printf("NO");
    }


    return 0;
}

bool IsSumOfPowers(long long int n)
{
    while (true)
    {
        if (n == 0)break;
        if (n % 3 == 2) {
            return false;
        }
        else {
            n /= 3;
        }
    }
    return true;
}