티스토리 뷰

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;
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함