A + B Problem II

Posted ixummer的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A + B Problem II相关的知识,希望对你有一定的参考价值。

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
 
AC代码:
 1 import java.math.BigDecimal;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5 
 6     public static void main(String[] args) {
 7         Scanner reader = new Scanner(System.in);
 8         int nTotal = reader.nextInt();   //nTotal为哪种情况
 9         for(int nCount = 0; nCount < nTotal; nCount++){
10             BigDecimal num1 = reader.nextBigDecimal();
11             BigDecimal num2 = reader.nextBigDecimal();
12             StringBuffer sbTemp = new StringBuffer("Case ");
13             sbTemp.append(nCount+1);
14             sbTemp.append(":\r\n");  //JAVA中的\r\n才是对应的回车换行
15             sbTemp.append(num1.toString());
16             sbTemp.append(" + ");
17             sbTemp.append(num2.toString());
18             sbTemp.append(" = ");
19             sbTemp.append(num1.add(num2).toString());
20             System.out.println(sbTemp.toString());
21             if(nCount != nTotal-1)
22                 System.out.println();
23         }
24     }
25 }

 

以上是关于A + B Problem II的主要内容,如果未能解决你的问题,请参考以下文章

大数加法 (A + B Problem II)

HDU oj A + B Problem II

C - A + B Problem II HDU - 1002

HDU - 1002 A + B Problem II (大整数加法)

J - A + B Problem II(第二季水)

A + B Problem II