博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SWUST OJ(961)
阅读量:4313 次
发布时间:2019-06-06

本文共 1687 字,大约阅读时间需要 5 分钟。

进制转换问题

1 #include
2 #include
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 
2 #include
3 4 using namespace std; 5 6 int main() 7 { 8 stack
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<

 

转载于:https://www.cnblogs.com/Ghost4C-QH/p/10589649.html

你可能感兴趣的文章
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>