HDU1002大整数相加Java解决

HDU1002大整数相加,因为题目告诉你了这些整数你不能用32位整数来表示出来,所以必须将它们转换为其他类型来处理。这里介绍两种JAVA解决的方法,第一种是利用JAVA类库提供的大整数类解决,另外一种是利用自定义类的方法解决。这里主要介绍自定义类的方法,因为Java类库提供的方法没什么好说的,只要学会用就行。而自定义类的方法可以修改成c/c++来解决,核心算法差不多。不多说,先上题:

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

使用Java类库解决代码:

package ACM;
import java.util.Scanner;
import java.math.BigInteger;
public class BigIntegerTest {
	public static void main(String []args) {
		Scanner in=new Scanner(System.in);
		int t=in.nextInt();
		for(int i=1;i<=t;i++) {
			BigInteger a,b,c;
			a=in.nextBigInteger();
			b=in.nextBigInteger();
			c=a.add(b);
			System.out.println("Case " + i + ":");
			System.out.println(a + " + " + b + " = " + c);
			if(i!=t)
				System.out.println();
		}
		in.close();
	}
}

使用自定义类解决代码:

package ACM;
//HDU1002
import java.util.Scanner;
public class IntegerAddOne {
	public String Add(String str1,String str2) {
		String str="";
		int len1=str1.length();
		int len2=str2.length();
		if(len1<len2) {//如果长度不一样则对位
			for(int i=0;i<len2-len1;i++)
			    str1="0"+str1;
		}
		else {
			for(int i=0;i<len1-len2;i++)
			    str2="0"+str2;
		}
		int cf=0;
		int temp;
		for(int i=str1.length()-1;i>=0;i--) {
			temp=str1.charAt(i)-'0'+str2.charAt(i)-'0'+cf;//计算第i位的值
			cf=temp/10;//第i位的进位
			temp%=10;//结果的第i位
			str=(char) (temp+'0')+str;
		}
		if(cf!=0) {
			str=(char) (cf+'0')+str;//最高位补1
		}
		return str;
	}
	public static void main(String []args) {
		Scanner in=new Scanner(System.in);
		int t=in.nextInt();
		in.nextLine();
		for(int i=1;i<=t;i++) {
			String str=in.nextLine();
			String Str[]=str.split(" ");
			String str1=Str[0];
			String str2=Str[1];
			IntegerAddOne demo=new IntegerAddOne();	//通过类去调用
			System.out.println("Case " + i + ":");
			System.out.println(str1 + " + " + str2 + " = " + demo.Add(str1, str2));
			if(i!=t)
				System.out.println();
		}
		in.close();
	}
}

str1和str2分别用来保存两个大整数对应的字符串,从右往左依次保存个位数,十位数,百位数…首先判断两个字符串的长度是否一致,若不一致,则需要在高位补零。然后从个位数开始,由于每次相加拿出来的都是字符,所以要逐位减去‘0’相加再加上进位值cf,cf的初始值为0,如果相加大于等于10则进位,用cf来保存进位值。等到所有的位都相加完了还要判断进位值是否为1,若为1即最后一次相加的结果还没进位,就要在最前面补1。最后结果就出来了。
这道题有些细节问题,它每个实例之间要用空行来隔开,所以在最后一个实例之前都要输出一行空行,最后一个实例则不需要。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值