有必要好好记一下这道题:我不小心入了很多细节坑。
1、首先,我使用了Java自带的 toHexString(int i)方法,此方法是返回的对应数据的补码,所以负数的时候它显示出来的就不是我们想要的那个结果。
——解决方法:负数采用取反+1,就会得到对应的正数原码,这时还不要忘记了给返回的结果手动加上一个“-” 负号
2、接着这个toHexString(int i)方法 自动转到的十六进制数据中的字母,是小写的,需要toUpperCase()
3、这一点我找了好久,算是我的一个不好的习惯,将int 改为 long,之后 AC
上代码:
package com.demo3;
import java.util.Scanner;
/*
* There must be many A + B problems in our HDOJ , now a new one is coming.
* Give you two hexadecimal(十六进制的) integers ,
* your task is to calculate the sum of them,and print it in hexadecimal too.
* Easy ? AC it !
*/
public class HDU_oj2057 {
public static char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B