zoukankan      html  css  js  c++  java
  • 十进制与八进制的转换(栈和队列)

    Description

    对于输入的任意一个非负十进制整数,利用栈打印输出与其等值的八进制数。

    Input

    111

    Output

    157

    Sample Input

    148

    Sample Output

    224

    先转换成2进制  在分3组存到另一个对列中(没理解转换,其实直接对8求余  直接取出就好,就当练手了)

    方法一

    #include <stdio.h>
    #include <string.h>
    #include <deque>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    deque <int> a,b;
    int main()
    {
        int n;
        scanf("%d",&n);
        while(n!=0)
        {
            a.push_back(n%2);
            n=n/2;
        }
        int p=a.size()/3;
        int q=a.size()%3;
        if(p==0)
        {
            for(int i=1;i<=p;i++)
            {
                int e=a.front();
                a.pop_front();
                int f=a.front();
                a.pop_front();
                int g=a.front();
                a.pop_front();
                int m=e+f*2+g*4;
                b.push_back(m);
            }
        }
        else
        {
            for(int i=1;i<=p+1;i++)
            {
                if(q!=p+1)
                {int e=a.front();
                a.pop_front();
                int f=a.front();
                a.pop_front();
                int g=a.front();
                a.pop_front();
                int m=e+f*2+g*4;
                b.push_back(m);}
                else
                {
                    if(q==1)
                    {
                        int e=a.front();
                        a.pop_front();
                        b.push_back(e);
                    }
                    if(q==2)
                    {
                        int e=a.front();
                        a.pop_front();
                        int f=a.front();
                        a.pop_front();
                        b.push_back(e+2*f);
                    }
                }
            }
        }
        int s=0,w=1;
        while(1)
        {
            if(b.empty())
                break;
            int x=b.front();
            b.pop_front();
            s+=x*w;
            w*=10;
        }
        printf("%d
    ",s);
        return 0;
    
    }

     

    方法二

    #include <stdio.h>
    #include <string.h>
    #include <deque>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    deque <int> a,b;
    int main()
    {
        int n;
        scanf("%d",&n);
        while(n!=0)
        {
            a.push_back(n%8);
            n=n/8;
        }
        int s=0,w=1;
        while(1)
        {
            if(a.empty())
                break;
            int x=a.front();
            a.pop_front();
            s+=x*w;
            w*=10;
        }
        printf("%d
    ",s);
        return 0;
    }
  • 相关阅读:
    Java中Collection和Collections的区别
    网站
    window.load 和$(document).ready() 、window.load和body onload区别
    『jQuery』.html(),.text()和.val()的使用
    jQuery选择器总结
    ios开发--编码格式
    iOS开发--基于AFNetWorking3.0的图片缓存分析
    iOS开发--沙盒路径与操作文件
    ios开发--第三方整理
    iOS 网络处理注意点
  • 原文地址:https://www.cnblogs.com/zcy19990813/p/9702770.html
Copyright © 2011-2022 走看看