- #include<iostream>
- #include<malloc.h>
- #include<stdlib.h>
- using namespace std;
- #define OK 1
- #define ERROR 0
- #define STACK_INIT_SIZE 100
- #define STACKINCREMENT 10
- #define OVERFLOW -2
- typedef int Status;
- typedef char ElemType;
- typedef struct
- {
- ElemType *top,*base;
- int stacksize;
- }SqStack;
- Status InitStack(SqStack &s) {
- s.base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
- if(!s.base) exit(OVERFLOW);
- s.top=s.base;
- s.stacksize=STACK_INIT_SIZE;
- return OK;}
- Status Push(SqStack &s,ElemType e) {
- if(s.top-s.base==s.stacksize) {
- s.base=(ElemType*)realloc(s.base,
- (s.stacksize+STACKINCREMENT)*sizeof(ElemType));
- if(!s.base) exit(OVERFLOW);
- s.stacksize+=STACKINCREMENT;}
- *s.top++=e;
- return OK;
- }
- Status Pop(SqStack &s,ElemType &e)
- {
- if(s.top==s.base) return ERROR;
- e=*--s.top;
- return OK;}
- void trans(int a,int b)
- { SqStack s;
- ElemType e;
- InitStack(s);
- while(a)
- {
- Push(s,a%b<10?'0'+a%b:a%b-10+'A');
- a/=b;
- }
- while(Pop(s,e))
- cout<<e;
- cout<<endl;}
- int main()
- {
- int a,b;
- while(cin>>a>>b)
- trans(a,b);
- return 0;}
- 8 2
- 1000
- 31 16
- 1F