题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1335
Problem Description
The Really Neato Calculator Company, Inc. has recently hired your team to help design their Super Neato Model I calculator. As a computer scientist you suggested to the company that it would be neato if this new calculator could convert among number bases. The company thought this was a stupendous idea and has asked your team to come up with the prototype program for doing base conversion. The project manager of the Super Neato Model I calculator has informed you that the calculator will have the following neato features:
It will have a 7-digit display.

Its buttons will include
the capital letters A through F in addition to the digits 0 through 9.

It
will support bases 2 through 16.

 
Input
The input for your prototype program will consist of
one base conversion per line. There will be three numbers per line. The first
number will be the number in the base you are converting from. The second number
is the base you are converting from. The third number is the base you are
converting to. There will be one or more blanks surrounding (on either side of)
the numbers. There are several lines of input and your program should continue
to read until the end of file is reached.
 
Output
The output will only be the converted number as it
would appear on the display of the calculator. The number should be right
justified in the 7-digit display. If the number is to large to appear on the
display, then print "ERROR'' (without the quotes) right justified in the
display.
 
Sample Input
1111000 2 10
1111000 2 16
2102101 3 10
2102101 3 15
12312 4 2
1A 15 2
1234567 10 16
ABCD 16 15
 
Sample Output
    120 
      78 
   1765
    7CA
  ERROR
  11001
 12D687
   D071
 
  1. #include<stdio.h>
  2. #include<cstring>.
  3. #include<iostream>
  4. using namespace std;
  5. char s[];
  6. int a,b;
  7. int len,i,j;
  8. long long c;
  9. char ans[];
  10. int f(int j)
  11. {
  12. int res=;
  13. while(j--)
  14. res*=a;
  15. return res;
  16. }
  17. int main()
  18. {
  19. while(cin>>s>>a>>b)
  20. {
  21. len=strlen(s);
  22. c=;
  23. for(i=len-,j=;i>=;i--,j++)
  24. {
  25. if(s[i]>=''&&s[i]<='')
  26. c+=(s[i]-'')*f(j);
  27. else
  28. c+=(s[i]-'A'+)*f(j);
  29. }
  30. //从a进制转化为十进制c
  31. //cout<<c<<" ";//
  32. //接下来从十进制c转换为b进制字符串ans
  33. for(i=;i<;i++)
  34. {
  35. if(c<)break;
  36. ans[i]=''+(c%b);
  37. c/=b;
  38. }
  39. int len1=i;
  40. //cout<<len1<<" ";//
  41. if(len1>)cout<<" ERROR"<<endl;
  42. else
  43. {
  44. for(i=;i<=-len1;i++)cout<<" ";
  45. for(i=len1-;i>=;i--)
  46. {
  47. if(ans[i]>=''&&ans[i]<='')
  48. cout<<ans[i];
  49. else
  50. printf("%c",ans[i]-''-+'A');
  51. }
  52. cout<<endl;
  53. }
  54. }
  55. return ;
  56. }

HDU 1335 Basically Speaking(进制转换)的更多相关文章

  1. HDOJ 1335 Basically Speaking(进制转换)

    Problem Description The Really Neato Calculator Company, Inc. has recently hired your team to help d ...

  2. HDU 2097 Sky数 进制转换

    解题报告:这题就用一个进制转换的函数就可以了,不需要转换成相应的进制数,只要求出相应进制的数的各位的和就可以了. #include<cstdio> #include<string&g ...

  3. HDU 5050 Divided Land(进制转换)

    题意  给你两个二进制数m,n   求他们的最大公约数  用二进制表示  0<m,n<2^1000 先把二进制转换为十进制  求出最大公约数  再把结果转换为二进制  数比較大要用到大数 ...

  4. HDU 2031 进制转换(10进制转R进制)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2031 进制转换 Time Limit: 2000/1000 MS (Java/Others)    M ...

  5. hdu 2031 进制转换(栈思想的使用)

    进制转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. HDU 1877 又一版 A+B(进制转换)

    看了http://lovnet.iteye.com/blog/1690276的答案 好巧妙的方法 递归实现十进制向m进制转换 #include "stdio.h" int m; v ...

  7. CF 552C 进制转换

    http://codeforces.com/problemset/problem/552/C C. Vanya and Scales time limit per test 1 second memo ...

  8. 进制转换,杭电0j-2031

    进制转换,杭电0j-2031原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=2031 [Problem Description] 输入一个十进制数N,将它 ...

  9. SQL Server 进制转换函数

    一.背景 前段时间群里的朋友问了一个问题:“在查询时增加一个递增序列,如:0x00000001,即每一个都是36进位(0—9,A--Z),0x0000000Z后面将是0x00000010,生成一个像下 ...

随机推荐

  1. Caffe安装过程错误处理方法

    1. 错误1: fatal error: caffe/proto/caffe.pb.h: No such file or directory 解决方法: You need to generate ca ...

  2. Java文档注释

    文档注释是用于生成API文档,API主要用于说明类.方法.成员变量 javadoc工具 处理文档源文件在类.接口.方法.成员变量.构造器和内部类之前的注释,忽略其他地方的文档注释.而且javadoc工 ...

  3. 2017年IT互联网圈跑会指南~

    啦啦啦~要放假啦,还有十多天就要过年啦,要走亲访友啦!相信大家也是各种胡吃海喝后,啊咧~腰上好像多了好几圈o(>﹏<)o为了让小伙伴们及时制定年后行程(减膘)计划,活动家特此奉上2017年 ...

  4. 将bat文件或exe程序注册成windows服务

    命令行使用sc命令.关于sc命令的详解,请自行查看帮助(sc /?),在此只简单提及如何加入系统服务功能.加入服务:sc create ServiceName binPath= 路径 start= a ...

  5. SQL sever 创建定时执行任务

    在SQL的使用过程中,我们经常要做些数据备份以及定时执行的任务. 这些任务能够帮助我们简化工作过程. 下面我们了解下如何创建一个定时执行的存储过程. 首先我们要打开 SQL server 代理服务 选 ...

  6. Storm 1.0.0

    Storm 1.0.0版本增加了很多新的特性,可用性以及性能也得到了很大的改善,该版本是Storm发展历程上一个里程碑式的版本,主要特点如下. 性能提升 Storm 1.0.0版本最大的亮点就是性能提 ...

  7. 【jQuery、原生】键盘键入两位小数

    jquery的处理办法 <!doctype html> <html lang="en"> <head> <meta charset=&qu ...

  8. 创建Properties文件

    /** * 生成Properties文件 * @param map Properties文件的内容,键值对 * @param path Properties文件生成后存放的路径 * @param pr ...

  9. Hive 执行计划

    执行语句 hive> explain select s.id, s.name from student s left outer join student_tmp st on s.name = ...

  10. [SOJ]连通性问题

    Description 关系R具有对称性和传递性.数对p q表示pRq,p和q是0或自然数,p不等于q.要求写一个程序将数对序列进行过滤,如果一个数对可以通过前面数对的传递性得到,则将其滤去.例如:输 ...