数据结构实验之栈与队列一:进制转换
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic Discuss
Problem Description
输入一个十进制非负整数,将其转换成对应的 R (2 <= R <= 9) 进制数,并输出。
Input
第一行输入需要转换的十进制非负整数;
第二行输入 R。
Output
输出转换所得的 R 进制数。
Sample Input
1279
8
Sample Output
2377
Hint
#include <iostream>
#include <bits/stdc++.h>
#define max 10000
using namespace std;
typedef int elemtype;
typedef struct
{
elemtype*top,*base;
int stacksize;
}sqstack;
void creat(sqstack*s)
{
s->base=(elemtype*)malloc(max*sizeof(elemtype));//申请max大小。
s->top=s->base;
s->stacksize=max;
}
void push(sqstack*s,int e)
{
*s->top=e; //指针前面加*为取该地址中的值
s->top++;
}
void zhuanhua(sqstack*s,int n,int m)
{
int t;
if(n == 0)
printf("%d\n",n);
else
while(n)
{
t=n%m;
push(s,t);
n=n/m;
}
}
void put(sqstack*s)
{
while(s->base != s->top)
{
s->top--;
printf("%d",*s->top);
}
printf("\n");
}
int main()
{
int n,r;
scanf("%d%d",&n,&r);
sqstack s; //定义结构体s.与顺序表一样.
creat(&s);
zhuanhua(&s,n,r);
put(&s);
return 0;
}