数据结构实验之栈与队列一:进制转换
Description
输入一个十进制非负整数,将其转换成对应的 R (2 <= R <= 9) 进制数,并输出。
Input
第一行输入需要转换的十进制非负整数;
第二行输入 R。
Output
输出转换所得的 R 进制数。
Samples
Sample #1
Input
1279
8
Output
2377
分析:
进栈出栈、进制转换
进栈时top先进再加
出栈时top先减再出
#include <bits/stdc++.h>
using namespace std;
typedef struct stack{
int *top,*base;
int len;
}Stack;
int n,m,a,b,c,t,k=0;
void initstack(Stack *s){
s->base=(int *)malloc(1000 * sizeof(int));
s->top=s->base;
s->len=1000;
}
void push(Stack *s,int x){
*s->top++=x;
}
int pop(Stack *s){
return *--s->top;
}
int main(){
cin>>n;
cin>>m;
Stack *s;
s=(Stack *)malloc(sizeof(Stack));
initstack(s);
if(n==0)
cout<<"0"<<endl;
while(n>0){
push(s,n%m);
n=n/m;
}
while(s->top!=s->base)
cout<<pop(s);
cout<<endl;
return 0;
}