CY_

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理


十进制与八进制的转换(栈和队列)

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;
}



posted on 2013-08-16 15:47  CY_  阅读(326)  评论(0编辑  收藏  举报