zoukankan      html  css  js  c++  java
  • SWUST OJ(961)

    进制转换问题

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 #define STACK_SIZE  100
     5 #define STCK_INCREMENT  10
     6 
     7 typedef struct 
     8 {
     9     int *base;
    10     int *top;
    11     int stacksize;
    12 }SqStack;
    13 
    14 void InitStack(SqStack &S)
    15 {
    16     //为栈申请空间
    17     S.base = (int*)malloc(STACK_SIZE * sizeof(int));
    18     if (!S.base)
    19     {
    20         exit(-2);
    21     }
    22     S.top = S.base;
    23     S.stacksize = STACK_SIZE;
    24 }
    25 
    26 
    27 void Push(SqStack &S, int e)
    28 {
    29     if ((S.top - S.stacksize) >= S.base) //栈满追加空间
    30     {
    31         S.base = (int*)realloc(S.base, (S.stacksize + STCK_INCREMENT) * sizeof(int));
    32         if (!S.base)
    33         {
    34             exit(-2);
    35         }
    36         S.top = S.base + S.stacksize;
    37         S.stacksize += STCK_INCREMENT;
    38     }
    39 
    40     *S.top++ = e;
    41 }
    42 
    43 int Pop(SqStack &S, int &e)
    44 {
    45     if (S.base == S.top)
    46     {
    47         return 0;
    48     }
    49     e = *--S.top;
    50     return 1;
    51 }
    52 
    53 
    54 int main()
    55 {
    56     int n, e;
    57     scanf("%d", &n);
    58     SqStack S;
    59     InitStack(S);
    60     while (n)
    61     {
    62         Push(S, n % 2);
    63         n = n / 2;
    64     }
    65     
    66     while (1)
    67     {
    68         int status = Pop(S, e);
    69         if (!status)
    70         {
    71             break;
    72         }
    73         printf("%d", e);
    74     }
    75     return 0;
    76 }

     方法二:

     1 #include <iostream>
     2 #include <stack>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     stack<int> mstack;
     9 
    10     int n;
    11     cin>>n;
    12     while(n)
    13     {
    14         mstack.push(n%2);
    15         n = n/2;
    16     }
    17 
    18     while(!mstack.empty())
    19     {
    20         cout<<mstack.top();
    21         mstack.pop();
    22     }
    23     return 0;
    24 }
  • 相关阅读:
    如何获取antlr解析的错误消息?(完美)
    2021最新版保姆级Journal Citation Reports使用
    【 Linux系统 】Centos下Linux启动时默认内核设置机制
    留言板
    友链
    java enum简记
    java enum多路分发下浅识表驱动式编程
    c语言建立和调用dll
    scanf
    js学习1 —— 类型,值,变量
  • 原文地址:https://www.cnblogs.com/Ghost4C-QH/p/10589649.html
Copyright © 2011-2022 走看看