数据结构实验之栈一:进制转换
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
输入一个十进制整数,将其转换成对应的R(2<=R<=9)进制数,并输出。
Input
第一行输入需要转换的十进制数;
第二行输入R。
第二行输入R。
Output
输出转换所得的R进制数。
Example Input
1279 8
Example Output
2377
Code realization
#include <stdio.h>
#include <stdlib.h>
#define maxn 100000
typedef int SElemType;
typedef struct node
{
SElemType *base; //栈底指针
SElemType *top; //栈顶指针
int stacksize; //当前栈的最大容量
}SqStack;
void InitStack(SqStack &S)
{
S.base = (SElemType *)malloc(maxn*sizeof(SElemType));
S.top = S.base;
S.stacksize = maxn;
}
void creat(SqStack &S, int n,int m)
{
while(n)
{
*S.top = n%m;
n = n/m; // n/=m
S.top++;
}
}
void output(SqStack &S)
{
while(S.top>S.base)
{
S.top--;
printf("%d",*S.top);
}
}
int main()
{
int n,m;
SqStack S;
scanf("%d %d",&n,&m);
InitStack(S);
creat(S,n,m);
output(S);
return 0;
}