杭电ACM第1002题(A + B Problem II)答案 java版

方法一:

package hduacm;

import java.util.Scanner;
public class Main{
    public static void main(String[] args) throws Exception{
         Scanner in = new Scanner(System.in);
         int n = in.nextInt();  
         int up = 0;   //进位
         int ma = 0;   //进位后的尾数
         int j = 1;    //case的编号

         while (n > 0) {
             StringBuffer temResult = new StringBuffer();
             up = 0;
             String a = in.next();  //第一个数字
             String b = in.next();  //第二个数字

             //获取a和b的长度
             int al = a.length();
             int bl = b.length();

             al--;
             bl--;
             //从a和b的最低位开始相加
             for(; al >= 0 && bl >= 0; al--, bl--){

                int temp1 = Integer.parseInt(a.charAt(al)+"");
                int temp2 = Integer.parseInt(b.charAt(bl)+"");
                int temp3 = temp1 + temp2 + up;

                if(temp3 >= 10){
                    up = temp3 / 10; 
                    ma = temp3 % 10;
                }else{
                    up = 0;
                    ma = temp3;
                }
                temResult.append(ma);
             }

             //两个数位数不一致,第1个数位数长
             while(al >= 0){
                 //或去第一个数的下一位数字
                 int temp1 = Integer.parseInt(a.charAt(al)+"");
                 //加上进位
                 int temp3 = temp1 + up;
                 //插入结果的下一位
                 temResult.append(temp3);
                 //将进位设为0
                 up = 0;
                 al--;
             }
             //两个数位数不一致,第2个数位数长
             while(bl >= 0){
                 int temp1 = Integer.parseInt(b.charAt(bl)+"");
                 int temp3 = temp1 + up;

                 temResult.append(temp3);
                 up = 0;
                 bl--;
             }
             //两个数位数一样,直接将进位插入结果的下一位
             if(up != 0){
                 temResult.append(up);
             }

             //将结果转为正序排列
             int len = temResult.length();
             char[] result = new char[len];
             len--;
             for(int i = 0; len >= 0; i++, len--){
                result[i] = temResult.charAt(len);
             }

             //输出
             System.out.println("Case "+j+":");
             System.out.print(a + " +" + " " + b + " = " +
              String.valueOf(result));

             if(n != 1){
                 System.out.println();
             }
             System.out.println();
             n--;
             j++;
          }  
    }
}

方法二:使用BigDecimal 类

import java.math.BigDecimal;
import java.util.Scanner;

public class Main{
    public static void main(String args[])     { 
        Scanner scanner = new Scanner(System.in);   
        String temp1=null;   
        String temp2=null;   
        String result=null;   
        int i; 

        int a=scanner.nextInt();   
        for(i=0;i<a;i++){    
            temp1=scanner.next();    
            temp2=scanner.next(); 
            BigDecimal bigdecimal=new BigDecimal(temp1);    
            BigDecimal bigdecimal2=new BigDecimal(temp2);    

            result=bigdecimal.add(bigdecimal2).toString();  
            if(i!=(a-1)) {
                System.out.println("Case"+" "+
                (i+1)+":\r\n"+bigdecimal+" + 
                "+bigdecimal2+" = "+result+"\r\n");
            } 
            else {
                System.out.println("Case"+" "+(i+1)
                +":\r\n"+bigdecimal+" +
                 "+bigdecimal2+" = "+result);
            }   
        }     
    } 
} 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值