Write a program to convert a whole number specified in any base (2..16) to a whole number in any other base (2..16). “Digits” above 9 are represented by single capital letters; e.g. 10 by A, 15 by F, etc.

Input

Each input line will consist of three values. The first value will be a positive integer indicating the base of the number. The second value is a positive integer indicating the base we wish to convert to. The third value is the actual number (in the first base) that we wish to convert. This number will have letters representing any digits higher than 9 and may contain invalid “digits”. It will not exceed 10 characters. Each of the input values on a single line will be separated by at least one space.

Output

Program output consists of the original number followed by the string ‘base’, followed by the original base number, followed by the string ‘=’ followed by the converted number followed by the string ‘base’followed by the new base. If the original number is invalid, output the statement

original_V alue is an illegal base original_Base number

where original_V alue is replaced by the value to be converted and original_Base is replaced by the original base value.

Sample input

2 10 10101

5 3 126

15 11 A4C

Sample output

10101 base 2 = 21 base 10

126 is an illegal base 5 number

A4C base 15 = 1821 base 11

Regionals 1990 >> North America - Southeast USA

问题链接:UVA355 UVALive5249 The Bases Are Loaded

问题简述:(略)

问题分析

进制转换问题,不解释。有关解释参见参考链接。

程序说明:(略)

题记

把功能封装到函数中是个好主意。

参考链接:POJ NOI0113-01 数制转换(Bailian2710)【进制】

AC的C++语言程序如下:

/* UVA355 UVALive5249 The Bases Are Loaded */#include <bits/stdc++.h>using namespace std;#define N 72
char s[N], ans[N];void convert(int base1, char s[], int base2)
{long val, dcount, digit;int i, j;val = 0;for(i=0; s[i]; i++) {if(isdigit(s[i]))val = val * base1 + s[i] - '0';elseval = val * base1 + toupper(s[i]) - 'A' + 10;}dcount = 0;while(val) {digit = val % base2;val /= base2;ans[dcount++] = ((digit >= 10) ? 'A' - 10 : '0') + digit;}if(dcount == 0) {ans[dcount++] = '0';ans[dcount] = '\0';} elseans[dcount] = '\0';// reversefor(i=0, j=dcount-1; i<j; i++, j--) {char c = ans[i];ans[i] = ans[j];ans[j] = c;}
}bool check(int base, char s[])
{for(int i=0; s[i]; i++) {int d = isdigit(s[i]) ? s[i] - '0' : s[i] - 'A' + 10;if(d >= base)return false;}return true;
}int main()
{int b1, b2;while(~scanf("%d%d%s", &b1, &b2, s)) {if(check(b1, s)) {convert(b1, s, b2);printf("%s base %d = %s base %d\n", s, b1, ans, b2);;} elseprintf("%s is an illegal base %d number\n", s, b1);}return 0;
}

UVA355 UVALive5249 The Bases Are Loaded【进制】相关推荐

  1. 枚举 + 进制转换 --- hdu 4937 Lucky Number

    Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) T ...

  2. js模拟栈---进制转化。十进制转任意进制进制,任意进制转十进制

    var Stack = (function(){var items = new WeakMap();//先入后出,后入先出class Stack{constructor(){items.set(thi ...

  3. JavaScript实现在线进制转换工具网站 -toolfk程序员在线工具网

    本文要推荐的[ToolFk]是一款程序员经常使用的线上免费测试工具箱,ToolFk 特色是专注于程序员日常的开发工具,不用安装任何软件,只要把内容贴上按一个执行按钮,就能获取到想要的内容结果.Tool ...

  4. hdu 4937 Lucky Number(数学题 进制转换)2014多校训练第7场

    Lucky Number                                                                          Time Limit: 20 ...

  5. php 10进制位数保持,php 任意进制的数转换成10进制功能实例

    /** * 任意进制的数转换成十进制 * @param * @arrange (512.笔记) jb51.cc * 显示转换数字所涉及的步骤 * 从任何基数(如八进制或十六进制)到基数10 * 请参阅 ...

  6. Python实现各种进制转换问题,so easy

    Python实现进制转换问题,so easy ********************十进制转其他进制************************************* #系统默认a,b值都是 ...

  7. Python求解进制问题(阿里巴巴2015笔试题)

    问题描述:用十进制计算30的阶乘,然后把结果转换成三进制表示,那么该进制表示的结果末尾会有多少个连续0? 解析:作为笔试题的话,要想按照题意先把阶乘结果计算出来再转换成三进制最后再数0的个数,时间肯定 ...

  8. JavaScript实现在线进制转换工具网站 -toolfk程序员工具网

    本文要推荐的[ToolFk]是一款程序员经常使用的线上免费测试工具箱,ToolFk 特色是专注于程序员日常的开发工具,不用安装任何软件,只要把内容贴上按一个执行按钮,就能获取到想要的内容结果.Tool ...

  9. c语言进制转换实验报告,c语言_各种进制转换.docx

    c语言_各种进制转换.docx c 语言 各种进制转换 计算机中常用的数的进制主要有二进制.八进制.十六进制. 2 进制,用两个阿拉伯数字0.1: 8 进制,用八个阿拉伯数字0.1.2.3.4.5.6 ...

最新文章

  1. SpringSecurity使用 配置文件 和wen.xml 文件配置
  2. OpenCvSharp_FindContours函数参数详解
  3. 《Python Cookbook 3rd》笔记(3.6):复数的数学运算
  4. kibana安装与基础用法
  5. github地址持续收集
  6. 求cluster的质心坐标
  7. c++中获取蓝图组件_Vue组件通信方式居然有这么多?你了解几种
  8. led闪烁和流水灯代码
  9. iOS 友盟分享(微信)
  10. w ndows模拟器,DS4Windows模拟器
  11. c语言hypot函数,hypot()函数以及C ++中的示例
  12. Social LSTM: Human Trajectory Prediction in Crowded Spaces 论文翻译
  13. Oracle误删除表空间的恢复
  14. Jenkins 登录忘记用户名和密码
  15. oracle共享函数,oracle常用函数及示例分享
  16. 【NLP】维基百科中文数据训练word2vec词向量模型——基于gensim库
  17. 实现判断商品折扣价格
  18. 关于单精度和双精度实数
  19. Excel函数公式:IF函数和AND、OR函数的组合多条件判断技巧
  20. Spring自动装配@Autowired的三种方式

热门文章

  1. c# mysql 连接类_c#中连接数据库的类怎么写呀?
  2. 产品经理如何搞定客户和业务
  3. reentrantlock原理_分享:synchronized和ReentrantLock的实现原理知识点
  4. python编程从入门到实战的16堂课_Python编程从入门到实战的16堂课(第2版)简介,目录书摘...
  5. 如何用法向量求点到平面距离_无论大考小考,无论校考,还是联考,老师钟爱的题型“空间距离”...
  6. idea中 mybatis 的 mapper.xml 新建没有 头文件
  7. Spark面试:Spark on yarn 运行流程
  8. 人员基础信息一体化采集仪_注意!会计人员信息采集,哪些人需要采集?
  9. 渲染进行调用_UE渲染师Dyomin:做次世代手游,可以用好这项技能
  10. Bootstrap-Table事件和方法