十进制与八进制的转换(栈和队列)
Description
对于输入的任意一个非负十进制整数,利用栈打印输出与其等值的八进制数。
Input
111
Output
157
Sample Input
148
Sample Output
224
思想很简单,n除8取余,利用栈先进后出的特点成功实现10进制到8进制的转换
代码:
#include <iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> s;
int n;
while(cin>>n&&n>0)
{
stack<int> s;
while(n>0)
{
int x = n%8;
s.push(x);
n/=8;
}
while(!s.empty())
{
cout<<s.top();
s.pop();
}
cout<<endl;
}
return 0;
}