进制转换
2015-06-09 10:23
225 查看
进制转换
TimeLimit: 1000ms Memory limit: 65536K
题目描述
输入一个十进制数N,将它转换成R进制数输出。
输入
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16,R<>10)。
输出
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
示例输入
72
2312
-43
示例输出
111
1B
-11
TimeLimit: 1000ms Memory limit: 65536K
题目描述
输入一个十进制数N,将它转换成R进制数输出。
输入
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16,R<>10)。
输出
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
示例输入
72
2312
-43
示例输出
111
1B
-11
//模拟栈 #include <map> #include <set> #include <cmath> #include <stack> #include <queue> #include <cstdio> #include <cctype> #include <vector> #include <string> #include <climits> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #define RR freopen("input.txt","r",stdin) #define WW freopen("output.txt","w",stdout) #define INF 0x3f3f3f3f using namespace std; struct node { char data; node *next; }; int main() { int n,m; node *head,*p; head=new node; while(~scanf("%d %d",&n,&m)) { if(n==0)//注意零 { cout<<0<<endl; } else { if(n<0) { cout<<'-'; n=-n; } int ans; head->next=NULL; while(n) { p=new node; ans=n%m; if(ans>=0&&ans<=9) { p->data=ans+'0'; } else { p->data=ans-10+'A'; } p->next=head->next; head->next=p; n/=m; } p=head->next; while(p) { cout<<p->data; p=p->next; } cout<<endl; } } return 0; }
STL #include <map> #include <set> #include <cmath> #include <stack> #include <queue> #include <cstdio> #include <cctype> #include <vector> #include <string> #include <climits> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #define RR freopen("input.txt","r",stdin) #define WW freopen("output.txt","w",stdout) #define INF 0x3f3f3f3f using namespace std; int main() { int n,m; while(~scanf("%d %d",&n,&m)) { if(n==0) { cout<<0<<endl; continue; } if(n<0) { cout<<'-'; n=-n; } stack<char>B; int ans; while(n) { ans=n%m; if(ans>=0&&ans<=9) { B.push(ans+'0'); } else { B.push(ans-10+'A'); } n/=m; } while(!B.empty()) { cout<<B.top(); B.pop(); } cout<<endl; } return 0; }
相关文章推荐
- android开发环境安装与配置
- JVM学习笔记(一)------的基本结构
- 苹果宣布首批HomeKit智能家居设备将在6月上市
- 进制转换 分类: 栈和队列 2015-06-09 10:23 12人阅读 评论(0) 收藏
- mybatis---级联保存,级联删除
- MySQ查看和设置字符集,数据库,表,字段
- 如何获取Android唯一标识(唯一序列号)
- Android studio导入项目(出现的问题)
- UIWindow & UIWindowLevel
- Glog使用介绍与源码分析
- Python range()函数
- Leetcode[26]-Remove Duplicates from Sorted Array
- 数据结构和算法学习六,之非递归排序
- 用:hover伪类代替js的hover事件
- JS计算字符串所占字节数
- VS2010+QT5编程之QTableView同时删除选中的多行
- Android安卓中循环录像并检测内存卡容量
- Android编译系统参考手册
- 关于Adapter对数据库的查询、删除操作
- 文本计算器