十进制与八进制的转换(栈和队列)
时间: 1ms 内存:128M
描述:
对于输入的任意一个非负十进制整数,利用栈打印输出与其等值的八进制数。
输入:
111
输出:
157
示例输入:
148
示例输出:
224
提示:
参考答案(内存最优[748]):
#include <stdio.h>
#include <stdlib.h>
struct f
{
int max1;
int length;
int *base,*top;
};
int main()
{
int i,n,a,d,c;
struct f b;
b.base=b.top=(int*)malloc(sizeof(int)*6);
b.max1=6;
b.length=0;
scanf("%d",&a);
while(a!=0)
{
d=a&7;
*(b.top)=d;
b.top++;
b.length++;
a=a>>3;
}
b.top--;
for(i=0;i<b.length;i++)
{
printf("%d",*(b.top));
b.top--;
}
return 0;
}
参考答案(时间最优[0]):
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int num;
while(cin>>num){
stack <int> s;
while(num/8>0){
s.push(num%8); //倒序压倒栈里
num/=8;
}
s.push(num);
while(!s.empty()){
cout<<s.top();
s.pop();
}
cout<<endl;
}
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。