#include <bits/stdc++.h>
using namespace std;
typedef int ElemType;
class Stack{
private:
ElemType *up; /// 指向栈顶
ElemType *base; /// 指向栈底
ElemType length; /// size
public:
Stack(); /// 初始化实现栈的初始化
void push(ElemType x); /// 入栈
void pop(); /// 出栈
ElemType top(){return *(up - 1);} /// 返回栈顶元素
ElemType size(){return length;} /// 返回栈的大小
bool empty(){return 0 == length;} /// 判断栈是否为空
};
int main()
{
int n,R;
ios::sync_with_stdio(false);
while(cin >> n)
{
Stack Q;
cin >> R;
if(n == 0){
cout << 0;
}
while(n)
{
Q.push(n%R);
n = n/R;
}
while(!Q.empty())
{
ElemType x;
x = Q.top();
cout << x;
Q.pop();
}
cout << endl;
}
return 0;
}
Stack::Stack(){ /// 栈的初始化的具体实现
base = new ElemType;
up = base;
length = 0;
}
void Stack::push(ElemType x){ /// 入栈函数的具体实现
*up++ = x;
length++;
}
void Stack::pop(){ /// 出栈函数的具体实现
up--;
length--;
}
栈与队列:进制转换
最新推荐文章于 2022-07-07 10:36:57 发布