[백준] 11005번 진법 변환2 / C++
#문제
#풀이
#include <iostream>
#include <vector>
using namespace std;
vector<char> vec;
int main()
{
ios::sync_with_stdio(false); cin.tie(NULL);
int N, B;
cin >> N >> B;
int num = N;
int toConvert = B;
int remainder = num % toConvert;
while (num != 0)
{
remainder = num % toConvert;
num /= toConvert;
if (remainder < 10)
{
vec.push_back(remainder + 48);
}
else
{
vec.push_back(remainder + 55);
}
}
for (int i = vec.size()-1; i >= 0; --i)
{
cout << vec[i];
}
return 0;
}
#정리
자연수 N과 B를 받는다. 10진법 수 N을 B진법으로 바꾸는 문제. N이 0이 될 때까지 B로 나눠주고, 나머지를 char타입 vector 배열에 담았다. 나머지가 10보다 작으면 48을 더해 숫자로 저장되게, 아니라면 55를 더해 대문자 알파벳이 나오도록 코드를 작성했다. N이 0이 되었다면 반복문을 마치고 vector를 역순으로 출력하여 답을 출력했다.
Enjoy Reading This Article?
Here are some more articles you might like to read next: