1. 程式人生 > >A + B Problem II

A + B Problem II

cee ane 代碼 () each space ces put follow

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