import java.util.Scanner;
class Main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
Hexadecimal h1=new Hexadecimal();
Hexadecimal h2=new Hexadecimal();
while(sc.hasNext()){
h1.str=sc.next();
h2.str=sc.next();
System.out.println( h1.Calculate(h2.str) );
}
}
}
class Hexadecimal{
String str;
static int count=0;
static char[] number={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public Hexadecimal(){
this(" ");
}
public Hexadecimal(String str){
this.str=str;
}
public int[] parseInt(String str){//解析字符串
if(str.charAt(0)=='+'){
return parse(1,1,str);
}
if(str.charAt(0)=='-'){
return parse(-1,1,str);
}else{
return parse(1,0,str);
}
}
public int[] parse(int x,int n,String str){//x表示正负
int[] a=new int[str.length()-n+1];
char ch;
if(x==-1){
a[0]=-1;
}else{
a[0]=1;
}
for(int i=n,k=1;i<str.length();i++){
ch=str.charAt(i);
for(int j=0;j<16;j++){
if(ch==number[j]){
a[k++]=j*x;
break;
}
}
}
return a;
}
public String Calculate(String s){
int[] a=parseInt(this.str);
int[] b=parseInt(s);
int n=0;
int i=a.length-1;
int j=b.length-1;
if( i>=j ){
n =i+1;
}else{
n =j+1;
}
int[] num=new int[n];
for( ;i>=1&&j>=1;i--,j-- ){
num[--n] += a[i]+b[j];
num[n-1] += num[n]/16;
num[n] %=16;
}
/*//
System.out.println("------start-------");
print(a);
print(b);
print(num);
System.out.println("-----"+i+"---------"+j+"-------"+n);
//*/
if( i<1 ){
for( ;j>=1;j--){
num[--n] += b[j];
num[n-1] += num[n]/16;
num[n] %= 16;
}
/*//*
System.out.println("---1---");
print(num);
//*/
}
if( j<1 ){
for( ;i>=1;i--){
num[--n] += a[i];
num[n-1] += num[n]/16;
num[n] %=16;
}
/*//
print(num);
System.out.println("---2---");
//*/
}
/*//
System.out.println("-----"+i+"---------"+j+"-------"+n);
print(a);
print(b);
print(num);
System.out.println("-----end---------");
//*/
int count=100;
for(int k=0;k<num.length;k++){
if(num[k]!=0){
count=k;
break;
}
}
String str="";
if(count==100){
str="0";
return str;
}else if(num[count]<0){
for(int k=count;k<num.length-1;k++){
if(num[k+1]>0){
num[k] += 1;
num[k+1] -= 16;
}
}
str="-";
}else{
for(int k=count;k<num.length-1;k++){
if(num[k+1]<0){
num[k] -= 1;
num[k+1] += 16;
}
}
}
for(int k=count;k<num.length;k++){
for(int t=0;t<16;t++){
if( abs(num[k])==t ){
str += number[t];
break;
}
}
}
return str;
}
public int abs(int n){
if(n>=0){
return n;
}else{
return -n;
}
}
public void print(int[] a){
System.out.println("\n---------"+ count +"----------");
System.out.println("a.length="+a.length);
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println("\n---------"+ count +"----------");
count++;
}
}
A + B Again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18682 Accepted Submission(s): 8051
Problem Description
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 !
Give you two hexadecimal integers , your task is to calculate the sum of them,and print it in hexadecimal too.
Easy ? AC it !
Input
The input contains several test cases, please process to the end of the file.
Each case consists of two hexadecimal integers A and B in a line seperated by a blank.
The length of A and B is less than 15.
Each case consists of two hexadecimal integers A and B in a line seperated by a blank.
The length of A and B is less than 15.
Output
For each test case,print the sum of A and B in hexadecimal in one line.
Sample Input
+A -A +1A 12 1A -9 -1A -12 1A -AA
Sample Output
0 2C 11 -2C -90
Author
linle
Source