11576번: Base Conversion
문제접근🤔
- a진법 → 10진법 → b진법
- 10진법에서 b진법으로 바꿀때 높은 자리수의 숫자부터 출력을 해야하기 때문에 스택을 이용하여 값을 저장
놓쳤던 부분😅
코드😁
2208 KB
0 ms
#include <iostream>
#include <vector>
#include <cmath>
#include <stack>
int a, b;
int m;
std::vector<int> digit;
std::stack<int> answer;
void input_setting()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
}
void input()
{
std::cin >> a >> b;
std::cin >> m;
digit.resize(m);
for (int i = 0; i < m; i++)
std::cin >> digit[i];
}
void solution()
{
int tmp = 0;
for (int i = 0; i < m; i++)
tmp += digit[i] * pow(a, m - 1 - i);
while (tmp)
{
answer.push(tmp % b);
tmp /= b;
}
}
void print()
{
while (!answer.empty())
{
std::cout << answer.top() << " ";
answer.pop();
}
}
int main(void)
{
input_setting();
input();
solution();
print();
return (0);
}