프로그래머스 - 3진법 뒤집기

2022. 8. 21. 15:11코딩 공부 연습

반응형

훨씬 쉽게할 수 있을 것 같은데 좀 멍청하게 했다. 하지만 뭐 이정도야!

 

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int threefunc(int x)
{
    long long tmp = 1;
    for (int i = 0; i< x; i++)
        tmp*=3;
    return tmp;
}
int solution(int n) {
    long long answer = 0;
    long long tmpthree = 0;
    while (n > 0)
    {
        tmpthree = tmpthree*10 + n%3;
        n = n/3;
    }
    long long x = 0;
    while ( tmpthree> 0)
    {
        answer = answer + (tmpthree%10) * threefunc(x);
        tmpthree/=10;
        x++;
    }
    return answer;
}