acm自学Java——Kibbles "n" Bits "n" Bits "n" Bits/Basically Speaking

这周的作业都是用Java来处理进制问题、大数计算和素数问题。简单来说是跟数字打交道。

A certain frazzled programmer is writing a program that receives two numbers at a time in hexadecimal form, performs an addition or subtraction on them, and outputs the result in decimal form. However, the binary representation of the hexadecimal numbers also needs to be output, in the exact format shown by the sample output below. This programmer would gladly write the routine to do this himself, but every time he tries to do anything in base 2, he breaks out in hives. So if you write this little routine for him, he would be eternally grateful. You may assume the following: • The largest allowable hexadecimal number is FFF. • When subtracting, the second number will always be smaller than the first, i.e. no negative results. • The spacing in the input file will be uniform throughout, i.e. no spaces at the beginning of a line, and one space between each element.

Input The input for this program will come from a file. The format for the file is as follows: N (This is the number of expressions to compute) HEX1 (+ or -) HEX2 (The first expression) . . . HEX1 (+ or -) HEX2 (The nth expression)

Output The output file should be in the following format: BINARY 1 (+ or -) BINARY 2 = DECIMAL (first result) . . . BINARY 1 (+ or -) BINARY 2 = DECIMAL (nth result)

Sample Input

2

A + 3

AAA + BBB 

Sample Output

0000000001010 + 0000000000011 = 13

0101010101010 + 0101110111011 = 5733

附上AC码: 

import java.util.*;
public class Main {
    public static void main(String[] args) {
        int n;
        int a,b;
        Scanner sc=new Scanner(System.in);
        String a1,b2,f;
        n=sc.nextInt();
         
        while(n!=0) {
            a1=sc.next();
            f=sc.next();
            b2=sc.next();
            //System.out.println(a1+f+b2);
             
            a=Integer.parseInt(a1, 16);
            b=Integer.parseInt(b2, 16);
             
            int result;
             
             
             
            //System.out.println(Integer.toBinaryString(a));
            //System.out.println(Integer.toBinaryString(b));
             
            String aa,bb;
              
            aa=Integer.toBinaryString(a);
             
            bb=Integer.toBinaryString(b);
             
            long one=Long.parseLong(aa);
            long two=Long.parseLong(bb);
             
             
             
            char ff=f.charAt(0);
            if(ff=='+') {
                result=a+b;
            }
            else {
                result=a-b;
            }
             
            System.out.printf("%013d ", one);
            System.out.print(ff);
            System.out.printf(" %013d", two);
            System.out.println(" = "+result);
            //System.out.println(a+f+b);
             
             
             
            n--;
        }
    }
 
}

 这道题涉及到进制转换和输出时的格式控制问题。

int  num = Integer.parseInt(Stirng a,int b)可以把String型的b进制数a转换成十进制int型数。

String bin = Integer.toBinaryString(int a)可以把int型a转换成Stirng型的二进制数。

long num = Long.parseLong(Stirng a)可以把String型的a转换成long型。

在最后格式化输出时,和C/C++差不多,但是long在Java中用"%d"。

这道题就这些要点。感觉用Java写acm时用好多现有的函数可以拿来用。

 

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. It may have leading ‘0’s. 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

   ABCD 16 15

          03 13 10

Sample Output

120

78

1765

7CA

ERROR

11001

D071 3

 输入输出格式好难调啊,我随便弄的,反正题目网上很多啦。

AC码如下:

import java.util.*;
public class Main {
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()) {
            String num,ba,ai;
             
            num=sc.next();
            ba=sc.next();
            ai=sc.next();
             
            int base=Integer.parseInt(ba);
            int aim=Integer.parseInt(ai);
             
            int n=Integer.parseInt(num, base);
             
            String p = Integer.toString(n,aim);
            String out=p.toUpperCase();
            if(out.length()>7) {
                System.out.println("  ERROR");
            }
            else if(out.length()==7){
                System.out.println(out);
            }
            else {
                int i=7-out.length();
                for(int j=0;j<i;j++) {
                    System.out.print(" ");
                }
                System.out.println(out);
            } 
        }
 
    }
 
}

也上一题没什么区别,但是输出的时候要注意一下。用Integer.toString()转换成大于10进制数时,Java中是用小写英文字母来代替10,11,12...但是题目要求是大写用toUpperCase()转换一下就好了。

今天也写不完了。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值