알고리즘 문제풀기/인프런 강의 정답

53. K진수 출력 (스택 자료구조 직접 구현) [정렬 & 이분탐색(결정알고리즘) & 투포인트 알고리즘 & 스택]

코다람쥐 2022. 4. 24. 10:22

나의정답.

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;
template<typename T>
class Stack
{
public:
	T* arr = new int[10000];

	void Push(T num) {
		arr[Count] = num;
		Count++;
	}

	int Pop() {
		if (Count > 0) {
			Count--;
			return arr[Count];
		}

		return -1;
	}

	int GetCount() {
		return Count;
	}

private:
	int Count = 0;
};

int main(int argc, char** argv) {
	//freopen("input.txt", "rt", stdin);
	int n, k, tmp;
	scanf("%d %d", &n, &k);

	Stack<int>* stack = new Stack<int>();
	tmp = n;
	while (tmp > 0) {
		int a = tmp % k;
		stack->Push(a);
		tmp /= k;
	}

	while (stack->GetCount() > 0) {
		char c = stack->Pop();
		c < 10 ? printf("%c", c + 48) : printf("%c", c + 55);
	}
	delete stack;
}