HDU1002 Calculation A+B Problem II (Java)

HDU1002Java两种解 AC

Problem Description:题目来源
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3
.
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

Author
Ignatius.L

题目大概意思就是输入两个超大整数(大到若用基本数据类型则会溢出)对这两个数求和。

思路:这个问题难点就是这两个数可以非常大,32位的int型无法存储。
方法1:与c/c++不同Java中提供了BigInteger类来处理超大整数,BigDecimal类处理超大浮点数,所以第一种方法就是使用BigInteger类,非常简单,但是要注意题目的输出格式,最后一个Case输出结果后不要有空行,还有特别注意输出内容的大小写。
下附Java AC 代码:
要特比注意输出个数

import java.util.Scanner;//引入Scanner
import java.math.BigInteger;//引入BigInteger

public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		BigInteger number1,number2;
		for(int i = 1;i <= t; i++){
			System.out.println("Case "+i+":");
			number1 = sc.nextBigInteger();
			number2 = sc.nextBigInteger();
			System.out.println(number1+" + "+number2+" = "+number1.add(number2));
			if(i < t) System.out.println();
		}
	}
}

方法2:有些题目可能会有要求说不能使用BigInteger类,这是解决思路也并不太难,主要看个人对java字符串各种操作的掌握情况。
因为基本数据类型位数不足以存储超大整数,所以选择以字符串形式存储大整数,然后诸位相加,再输出结果,具体过程再代码注释中解释。
附Java AC 代码:

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		for(int i = 1;i <= t;i++){
			System.out.println("Case "+i+":");
			String number1 = sc.next();
			String number2 = sc.next();
			String result =addBigNumber(number1,number2);
			 System.out.println(number1+" + "+number2+" = "+result);
			 if( i < t) System.out.println();
		}
	}
	public static String addBigNumber(String num1,String num2){
		StringBuffer add = new StringBuffer();
		 /*逆序存储输入的字符串
        * 目的:两字符串长度不相同时将短字符串用0补齐
        * 因为数字是高为补0,字符串方便在字符串后补字符,所以先逆序*/
		String n1 = new StringBuffer(num1).reverse().toString();
		String n2 = new StringBuffer(num2).reverse().toString();
		 /*获取两字符串数字最大长度*/
		int maxLen = n1.length() > n2.length() ?  n1.length():n2.length();
		 /*如果n1比n2短,则n1高位用"0"补齐*/
		if(n1.length() < n2.length()){
			for(int i = n1.length(); i < n2.length(); i++){
				n1 += "0";
			}
		}
		/*如果n2比n1短,则n2高位用"0"补齐*/
		if(n1.length() > n2.length()){
			for(int i = n2.length(); i < n1.length(); i++){
				n2 += "0";
			}
		}
		int flag = 0;//进位标志
		boolean overFlow = false;//是否溢出,即加法结束后数字长度有没有变长
		for(int i = 0; i < maxLen; i++){
		 /*将字符串按字符转换为数字再进行加法运算*/
			int sum = Integer.parseInt(n1.charAt(i)+"")+Integer.parseInt(n2.charAt(i)+"")+flag;
			/*若相加大于等于10,则进位,结果字符串中加入sum-10*/
		if(sum >= 10){
		/*如果i是最高为则溢出*/
			if(i == maxLen){
				overFlow = true;
			}
			flag = 1;
			add.append(sum-10);
		}else{
		/*若相加小于10,则不进位,结果字符串中加入sum*/
			flag = 0;
			add.append(sum);
		}
	}
	//有溢出则最高为再加1
		if (overFlow){
            add.append(flag);
        }
         //最后返回逆序字符串,结果字符串就正序了
        return add.reverse().toString();
	}
}

欢迎留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ALIM MUSA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值