웅재의 코딩세상

[프로그래머스] 3진법 뒤집기 본문

코딩테스트/프로그래머스 - LV 1

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

웅드 2024. 2. 9. 17:06

 

  • 풀이
#include <string>
#include <vector>
#include <cmath>

using namespace std;

int solution(int n) {
    int answer = 0;
    vector<int> arr;
    while(n!=0){
        arr.push_back(n%3);
        n /=3;
    }
    int j = arr.size();
    for(int i=0; i<j; i++){
        answer = answer + arr[j-i-1] * pow(3,i);
    }
    return answer;
}
반응형