[BOJ][C#] 2745 진법 변환

LimJaeJun·2024년 1월 6일
0

PS/BOJ

목록 보기
88/108

📕 문제

📌 링크

📗 접근 방식

입력 처리

  • 숫자(number)와 진법(formation)을 각각 저장한다.

진법 변환

  • ConvertToInt 함수는 주어진 숫자와 진법을 이용하여 10진수로 변환한다.
  • 문자열의 가장 뒷자리부터 시작하여, 각 자릿수를 해당 진법에 맞게 10진수로 변환하여 더해준다.
  • 자릿수마다 해당 진법의 지수(formation^index)를 곱하여 더한다.

결과 출력

  • 변환된 10진수를 출력한다.

📘 코드

namespace BOJ
{
    class No_2745
    {
        static void Main()
        {
            string[] inputs = Input().Split();
            string number = inputs[0];
            int formation = int.Parse(inputs[1]);
            Console.WriteLine(ConvertToInt(number, formation));
        }
        static string Input() => Console.ReadLine();

        static int ConvertToInt(string number, int formation)
        {
            int index = 0;
            int ret = 0;
            for (int i = number.Length - 1; i >= 0; i--)
            {
                int temp = number[i] >= 'A' ? (number[i] - 'A') + 10 : int.Parse(number[i].ToString());
                ret += temp * (int)Math.Pow(formation, index);
                index++;
            }

            return ret;
        }
    }
}

📙 오답노트

ConvertToInt 함수 로직에서 temp에서 삼항연산자 조건문에서 A와 비교할 때 등호 부분 =를 빼먹어서 한 번 틀렸다.

📒 알고리즘 분류

  • 수학
  • 구현
  • 문자열
profile
Dreams Come True

0개의 댓글

관련 채용 정보