◎ 문제
○ 출처
programmers.co.kr/learn/courses/30/lessons/68935
○ 문제 설명
자연수 n이 매개변수로 주어집니다. n을 3진법 상에서 앞뒤로 뒤집은 후, 이를 다시 10진법으로 표현한 수를 return 하도록 solution 함수를 완성해주세요. |
○ 제한사항
|
○ 입출력 예
![]() |
○ 입출력 예 설명
![]() |
○ 작성 예시 코드
using System;
public class Solution {
public int solution(int n) {
int answer = 0;
return answer;
}
}
◎ 나의 문제풀이
using System;
using System.Collections.Generic;
public class Solution
{
public int solution(int n)
{
int answer = 0;
int divisor = 3;
int dividend = n;
Queue<int> result_stack = new Queue<int>(); //삼진법의 역순으로 데이터 조회를 위해 큐를 사용
// 삼진법 계산
while (dividend >= divisor)
{
result_stack.Enqueue(dividend % divisor);
dividend /= divisor;
}
// 가장 마지막 셈 숫자 & 3이하 숫자 처리
if (dividend > 0)
{
result_stack.Enqueue(dividend);
}
// 삼진법 -> 십진법 변환
while (result_stack.Count > 0)
{
int number = result_stack.Dequeue();
int pow = (int)Math.Pow(divisor, result_stack.Count);
answer += number * pow;
}
return answer;
}
}
'프로그래밍 문제 풀이 > C#' 카테고리의 다른 글
[프로그래밍 문제 풀이] 프로그래머스 - 내적 (C#) (0) | 2021.03.12 |
---|---|
[프로그래밍 문제 풀이] 프로그래머스 - 영어 끝말잇기 (C#) (0) | 2020.10.20 |
[프로그래밍 문제 풀이] 프로그래머스 - 두 개 뽑아서 더하기(C#) (0) | 2020.10.13 |
[프로그래밍 문제 풀이] 프로그래머스 - 최솟값 만들기 (C#) (0) | 2020.09.10 |
[프로그래밍 문제 풀이] 프로그래머스 - 다음 큰 숫자 (C#) (0) | 2020.09.10 |