반응형
링크
https://school.programmers.co.kr/learn/courses/30/lessons/68935?language=cpp
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 설명 및 제한 조건
나의 코드
#include <string>
#include <vector>
using namespace std;
int solution(int n) {
int answer = 0;
vector<int> TernaryScale;
int Decimal = n;
while(Decimal)
{
TernaryScale.push_back(Decimal % 3);
Decimal /= 3;
}
int temp = 1;
for(int i = TernaryScale.size() - 1; i >= 0; i--)
{
answer += TernaryScale[i] * temp;
temp *= 3;
}
return answer;
}
채점 결과
다수 코드
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int solution(int n) {
int answer = 0;
vector<int> v;
while(n)
{
v.push_back(n % 3);
n /= 3;
}
reverse(v.begin(), v.end());
for(int i = 0; i < v.size(); i++)
answer += pow(3, i) * v[i];
return answer;
}
반응형
'프로그래머스' 카테고리의 다른 글
[프로그래머스 C++] 문자열을 정수로 바꾸기 (0) | 2022.10.28 |
---|---|
[프로그래머스 C++] 시저 암호 (0) | 2022.10.25 |
[프로그래머스 C++] 이상한 문자 만들기 (0) | 2022.10.22 |
[프로그래머스 C++] 직사각형 별찍기 (0) | 2022.10.18 |
[프로그래머스 C++] 최대공약수와 최소공배수 (0) | 2022.10.17 |