문제 : https://www.acmicpc.net/problem/11576


[ 알고리즘풀이 ]

 A진법으로 표현한 수를 10진법으로 돌리고, 다시 그 수를 B진법으로 표현하면 된다.

#include<iostream>
#include<vector>
using namespace std;

int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int A, B, M, list[25] = {};
	cin >> A >> B >> M;
	for (int i = 0; i < M; i++)
		cin >> list[i];
	
	// A진법 수를 10진법으로
	int temp = 0, mul = 1;
	for (int i = 0; i < M; i++) {
		temp += mul * list[M - 1 - i];
		mul *= A;
	}
	// 10진법으로 표현한 수를 B진법으로
	vector<int> ans;
	while (temp) {
		ans.push_back(temp % B);
		temp /= B;
	}

	for (int i = 0; i < ans.size(); i++)
		cout << ans[ans.size() - 1 - i] << ' ';

	return 0;
}

 

+ Recent posts