数据结构实验之栈与队列一:进制转换
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
输入一个十进制非负整数,将其转换成对应的 R (2 <= R <= 9) 进制数,并输出。
Input
第一行输入需要转换的十进制非负整数;
第二行输入 R。
Output
输出转换所得的 R 进制数。
Sample Input
1279
8
Sample Output
2377
Hint
Source
#include <stdio.h>
#include <stdlib.h>
int date[101]; //初始化栈的大小
int main()
{
int a,r,top=0,base=0; //top=base=0时为空栈
scanf("%d%d",&a,&r);
if(a==0)
printf("%d\n",a); //0的任意进制均为0
else
{
while(a!=0)
{
date[top++]=a%r; //进栈(top始终在栈顶元素上方)
a=a/r;
}
while(top!=0)
printf("%d",date[--top]); //出栈
}
return 0;
}
/* 利用十进制转换r进制算法
最小位=十进制数%r 其余位为十进制数/r%r */
总结:利用栈先进后出的特点输出。