使用栈实现进制转换
2015-06-17 12:12
411 查看
Problem D: 使用栈实现进制转换
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 12 Solved: 9
[Submit][Status][Web
Board]
Description
使用栈将一个很长(>30)的十进制数转换为二进制数
Input
若干个很长的十进制数每行一个
Output
转换为二进制,每行输出一个
Sample Input
123456789012345678901234567890 753951684269875454652589568545854758545824
Sample Output
1100011101110100100001111111101101100001101110011111000001110111001001110001111110000101011010010 10001010011110101010001101001000100100100000101001011010001010101001000100111101011001110001000111110010001000101101111110110110100110100000
#include<cstdio> #include<iostream> #include<cstring> using namespace std; char s[10010]; int a[10010]; int b[10010]; int main() { int stl,i,start,flag,flag1,num,n; while(cin>>s) { num=start=0; stl=strlen(s); for(i=0;i<stl;i++) { a[i]=s[i]-'0'; // cout<<a[i]; } // cout<<endl; while(stl-start!=1) { // cout<<"start="<<start<<"and a[start]="<<a[start]<<endl; flag=a[start]%2; a[start]/=2; for(i=start+1;i<stl-1;i++) { flag1=flag*10+a[i]; flag=flag1%2; a[i]=flag1/2; } flag1=flag*10+a[stl-1]; a[stl-1]=flag1/2; b[num++]=flag1%2; if(a[start]==0) { start++; } /* for(i=start;i<stl;i++) { cout<<a[i]; } cout<<endl;*/ } n=a[start]; while(n>0) { b[num++]=n%2; n/=2; } for(i=num-1;i>=0;i--) { cout<<b[i]; } cout<<endl; } }
相关文章推荐
- UIManager的字体颜色参数对照表
- Apache Shiro 学习记录1
- 把Bitmap写到SD卡后,更新系统相册时发生OutOfMemoryError
- Linux进程通信之信号
- iOS开发,谓词(NSPredicate)的用法:(二)谓词和正则表达式配合使用,进行表单内容判断
- 一个带有函数的类占几个字节
- CSS 文字上下颠倒显示
- Ketama Consisent Hash
- Java读取“桌面”、“我的文档”路径的方法
- 在Python的struct模块中进行数据格式转换的方法
- 请输出以下4*5矩阵
- AngularJS $http 异步后台无法获取请求参数
- Runtime.getRuntime().exec(...),当参数中有空格时!
- 跳槽速度快、薪资涨幅大的热门职位
- GridControl常见用法
- 23设计模式(5):原型模型
- 终于解决了用JAVA写窗口程序在不同的windows界面下的显示保持一致。
- codeigniter 已经过时了吗?
- 利用数据库介质文件创建额外域控制器
- 记AWSome Day Shanghai 150612