알고리즘/[문제풀이] BOJ

[BOJ] 11005 - 진법 변환 2

be-lgreen 2021. 9. 30. 00:41

https://www.acmicpc.net/problem/11005

 

11005번: 진법 변환 2

10진법 수 N이 주어진다. 이 수를 B진법으로 바꿔 출력하는 프로그램을 작성하시오. 10진법을 넘어가는 진법은 숫자로 표시할 수 없는 자리가 있다. 이런 경우에는 다음과 같이 알파벳 대문자를

www.acmicpc.net

 

난이도

기본구현 / 난이도 ⭐️ 

 

문제 풀이

n을 k진법으로 변환하기 위한 코드를 정리하기 위해 풀어봤다.

 

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

using namespace std;

int main()
{
    int n, b;
    scanf("%d %d", &n, &b);

    int rest;
    string result = "";
    while (n > 0)
    {
        rest = n % b;
        n = n / b;

        if (rest < 10)
        {
            result = to_string(rest) + result;
        }
        else
        {
            result = (char)('A' + rest - 10) + result;
        }
    }

    cout << result << endl;
}
댓글수0