杭电oj————2057(java)

question:A+ B again

思路:额,没啥思路/捂脸,用java的long包里的方法,很简单,只是有几次WA,有几点要注意一下

注意:如果数字有加号要删除掉,这里用到了正则表达式“\+”来匹配加号(我在自己的电脑里不用删除加号也可以通过,杭电oj上就过不了,必须要加这个……)

source code:

package hduoj;

import java.util.Scanner;

public class hdoj_2057 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String the_first = sc.next();
            String the_last = sc.next();
            long the_first_one = Long.parseLong(the_first.replaceAll("\+",""),16);
            long the_last_one = Long.parseLong(the_last.replaceAll("\+",""),16);
            long sum = the_first_one + the_last_one;
            //String the_res = Integer.toHexString(Math.abs(sum)).toUpperCase();
            if(sum<0){
                System.out.println("-" + Long.toHexString((-1)*sum).toUpperCase());
            }else{
                System.out.println(Long.toHexString(sum).toUpperCase());
            }

        }
    }
}

注:代码已经AC,贴上来的是在自己编译器里的,需要改关键字

希望对大家有所帮助

以上

原文地址:https://www.cnblogs.com/lavender-pansy/p/12177300.html