HDOJ 1335 Basically Speaking(进制转换)

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

输入3个数:n a b

输入的n是a进制的数,需要把n再转换成b进制的数。

如果转换之后的数字位数超过了7,就输出“ ERROR”。

每个输出占7位,不足7位的左边补空格。

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String strNum = sc.next();
            int a = sc.nextInt();
            int b = sc.nextInt();

            int s = Integer.valueOf(strNum, a);
            //将a进制的strNum转换成10进制s
            String sNum = Integer.toString(s, b);

            if(sNum.length()>7){
                System.out.println("  ERROR");
                continue;
            }

            for(int i=7;i>sNum.length();i--){
                System.out.print(" ");
            }
            System.out.println(sNum.toUpperCase());

        }

    }

}
时间: 2024-08-02 14:00:35

HDOJ 1335 Basically Speaking(进制转换)的相关文章

hdoj 2031 进制转换

进制转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30304    Accepted Submission(s): 16811 Problem Description 输入一个十进制数N,将它转换成R进制数输出. Input 输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<&

任意进制转换算法

任意进制转换算法 N年没有写博客,发个进制转换的玩下,支持负数:功能属于简化版的 Convert.ToInt32 ,特点是: 1.任意位,如:0,1(二进制),0...7(八进制),0...9,A...F(16进制),0...N(N进制),或者是:[email protected]#$%^&*(8进制,字符符号),也可以是中文. 2.8 byte 最大长度. 3.C#源码. 最近写markdown格式习惯啦,cnblogs啥时候全改掉算了,别用这个htmleditor算了. 先说明下进制转换的基

03 php 数据类型:整数,进制转换,浮点,字符,布尔,数组,空类型,类型转换,算术运算,比较运算

03 数据类型:整数,进制转换,浮点,字符,布尔,数组,空类型,类型转换, 算术运算,比较运算,逻辑运算,短路现象, 三目运算符,字符型运算: 数据类型 整体划分 标量类型: int, float, string, bool 复合类型: array,     object 特殊类型: null,     resouce 整数类型int, integer 3种整数表示法 十进制写法:123: $n1 = 123; 八进制写法: 0123 $n2 = 0123; 十六进制写法: 0x123 $n3

计算机进制转换

一.计算机只认识0和1,二进制. 二.2进制转换成 8进制 和 16进制,如下图: 二进制 > 八进制 :  研究上图发现,3位最高二进制可以用来表示一位八进制.所以,将二进制分解每3位,不够前面补0,然后每3位转换为10进制,顺序排列即可. 二进制 > 十六进制  :4位最高二进制可以用来表示一位十六进制.所以,将二进制分解每4位,不够前面补0,然后每4位转换为10进制,超过9用字母表示即可.顺序排列即可. 如下: 二进制 > 十进制:   11001001 = 2^7+2^6+2^3

原理之一,进制转换

原理之一,进制转换 日常生活中采用个数字都是十进制,而计算机采用的是运算更简单.易实现且可靠,为逻辑设计提供了有力途经的二进制,除此之外还有八进制和十六进制作为二进制的缩写. 进制:逢N进一,N是每种进位计数制表示一位数所需要的符号数目为基数. 二进制:逢二进一,借一当二,包含的数字(0.1) 八进制:逢八进一,借八当一,包含(0.1.2.3.4.5.6.7) 十六进制:逢十六当一,以一当十六,包含(0.1.2.3.4.5.6.7.8.9.10(A).11(B).12(C).13(D).14(E

NOIP2000 进制转换

题一   进制转换              (18分)  问题描述      我们可以用这样的方式来表示一个十进制数: 将每个阿拉伯数字乘以一个以该数字所处位置的(值减1)为指数,以10为底数的幂之和的形式.例如:123可表示为 1*102+2*101+3*100这样的形式. 与之相似的,对二进制数来说,也可表示成每个二进制数码乘以一个以该数字所处位置的(值-1)为指数,以2为底数的幂之和的形式.一般说来,任何一个正整数R或一个负整数-R都可以被选来作为一个数制系统的基数.如果是以R或-R为基

进制进制进制~转换

从刚学计算机器就对进制转换有着莫名的反感,2进制 8进制 10进制 16进制各种转换. 下面就说下逻辑地址转换成物理地址的求法吧 首先,用户输入一个16进制的数字cin>>hex>>logic_add;   hex的意思是告诉计算机输入的数是以16进制方式输入的 这个时候你要是输出cout<<logic_add;  你会发现输出的是把这个16进制的数转换为10进制以后输出的结果 cout<<hext<<logic_add;这样输出的才是16进制.

黑马程序员------进制转换

------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训..Net培训</a>.期待与您交流! ------- 在java中数字的表现形式一般有二进制,八进制,十进制,十六进制等,在平时的编程中我们可以通过java提供的API函数方便的实现各个进制间的转换,如:Integer.toHexString(int i)--十进制转十六进制:Integer

c语言之进制转换(栈实现)

从上两篇博客中我们可以知道,栈具有后进先出的特性,而进制转换的打印输出刚好与计算过程相反,满足栈这后进先出的特性, 所以可以用栈很快的实现进制转换,下面是用栈实现进制转换的c函数 void conversion (SqStack *pstack,unsigned int N, const unsigned int d){ if( pstack == NULL)//当传入参数为指针,必须判空 exit(-1); int mod ;//保存mod = N %d while( N != 0){ mod