hdu 2057 A + B Again




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 !
 

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.
 

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值