杭电ACM---1002(大数)

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 305668    Accepted Submission(s): 59062



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.
 

Outpu

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 Inputt
   
   
2 1 2 112233445566778899 998877665544332211
 

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

分析:本体表面上看只是普通的a+b题,要注意的是,输入的数可能大到整数甚至是浮点数无法表示,因此这一题有两种做法,如下。


做法一:用字符串和字符数组做

这种方法比较麻烦一些,有点绞思维

<span style="font-weight: normal;"><span style="font-size:14px;">import java.util.Scanner;

class Main{
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();
        int c=0;
        int num=0;
        while (n-->0){
        	num++;
        	String a=input.next();
        	String b=input.next();
        	char cha[]=a.toCharArray();
        	char chb[]=b.toCharArray();
        	int A[]=new int[1000];
        	int B[]=new int[1000];
        	int C[]=new int[1000];
        	for (int i=0;i<cha.length;i++){
        		A[i]=cha[cha.length-1-i]-48;
        	}
        	for (int i=0;i<chb.length;i++){
        		B[i]=chb[chb.length-1-i]-48;
        	}
        	int s=0;
        	for (int i=0;i<1000;i++){
        		s+=A[i]+B[i];
        		C[i]=s%10;
        		s/=10;
        	}
        	String str="";
        	int j;
        	for (j=999;j>=0;j--){
        		if (C[j]>0){
        			break;
        		}
        	}
        	for (int i=j;i>=0;i--){
        		str+=C[i];
        	}
        	if (c++>0){
        		System.out.println();
        	}
        	System.out.println("Case "+num+":");
        	for (int i=0;i<cha.length;i++){
        		System.out.print(cha[i]);
        	}
        	System.out.print(" + ");
        	for (int i=0;i<chb.length;i++){
        		System.out.print(chb[i]);
        	}
        	System.out.println(" = "+str);
        }
    }
}</span></span>

做法二:用java的大数做

这种就非常方便,且好懂,省时(关于大数详情请见本博客“Java之------大数(BigInteger,BigDecimal)点击打开链接)

<span style="font-size:14px;font-weight: normal;">import java.math.BigInteger;
import java.util.Scanner;

class Main{

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int t=input.nextInt();
		int blank=0;
		int num=1;
		while (t-->0){
			if (blank++>0){
				System.out.println();
			}
			String aStr=input.next();
			String bStr=input.next();
			
			BigInteger a=new BigInteger(aStr);
			BigInteger b=new BigInteger(bStr);
			BigInteger c=a.add(b);
			
			String cStr=c.toString();
			System.out.println("Case "+num+":");
			System.out.println(aStr+" + "+bStr+" = "+cStr);
			num++;
		}
	}
}</span>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值