博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构之(HDU2051 Bitset)
阅读量:7081 次
发布时间:2019-06-28

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

Problem Description
Give you a number on base ten,you should output it on base two.(0 < n < 1000)
Input
For each case there is a postive number n on base ten, end of file.
 
Output
For each case output a number on base two.
 

 

Sample Input
1
2
3
Sample Output
1
10
11
 
1.普通方法
代码如下:
1 #include "stdafx.h" 2 #include
3 #include
4 using namespace std; 5 int main() 6 { 7 int a[100]; 8 int n; 9 int m;10 int i;11 while(cin>>n)12 {13 i=0;14 while(n)15 {16 i=i+1;17 a[i]=n%2;18 n/=2;19 }20 for(i;i>0;i--)21 cout<

 

运用桟解决:

代码如下:

 

1 #include "stdafx.h" 2 #include
3 #include
4 using namespace std; 5 #define OK 1 6 #define ERROR 0 7 #define OVERFLOW -2 8 #define MAXSIZE 100 9 typedef int Status;10 typedef int SElemType;11 12 typedef struct13 {14 SElemType *base;15 SElemType *top;16 int stacksize;17 }SqStack;18 19 Status InitStack(SqStack &S) //桟的初始化20 {21 S.base = new SElemType[MAXSIZE];22 if (!S.base)23 exit(OVERFLOW);24 S.top = S.base;25 S.stacksize = MAXSIZE;26 return OK;27 }28 29 Status Push(SqStack &S, SElemType e) //进栈30 {31 if (S.top - S.base == S.stacksize)32 return ERROR;33 *S.top++ = e;34 return OK;35 }36 37 Status Pop(SqStack &S, SElemType &e) //出桟38 {39 if (S.top == S.base)40 return ERROR;41 e = *--S.top;42 return OK;43 }44 int main()45 {46 int N;47 SqStack S;48 SElemType e;49 InitStack(S);50 while (cin >> N)51 {52 while (N != 0)53 {54 e = N % 2;55 Push(S, e);56 N = N / 2;57 }58 while (!(S.top == S.base))59 {60 Pop(S, e);61 cout << e;62 }63 cout << endl;64 }65 return 0;66 }

 

 

 

转载于:https://www.cnblogs.com/Trojan00/p/8847810.html

你可能感兴趣的文章
pycurl mac 安装报错Curl is configured to use SSL,
查看>>
服务器部署多个tomcat方法
查看>>
android 重写TextView 实现文字整齐排版
查看>>
yii2的bootstrap配置字段理解
查看>>
解决旧版MySql无法存储emoji表情的问题
查看>>
Flask的SERVER_NAME解析
查看>>
Balanced Binary Tree
查看>>
Excel数据导出功能
查看>>
spring的三种注解管理器
查看>>
libreoffice转换文档的方法(支持各平台各版本的libreoffice)
查看>>
图片处理工具类
查看>>
Android Mms专题之:Compose详解
查看>>
Drawable 和 Bitmap(转)
查看>>
xpath解析
查看>>
elasticsearch spring 集成
查看>>
#Lubuntu#安装exscript时提示的错误
查看>>
常用正则表达式
查看>>
开源监控软件Hyperic 对 MongoDB的监控
查看>>
Java:如何检查枚举是否包含给定的字符串?
查看>>
Webstorm/Phpstorm中将ES6文件转为普通js文件
查看>>