杭电1002

这是改用java做题的第三题

想要记录一下做题时出现的傻傻的问题

1、输出的标点符号都是英文

2、有关进位的题目要注意进位、和进位之后的第二次进位

3、注意前导零的删除

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

下面 就是代码了:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        for(int i = 1;i<=a ;i++) {
            int mark = 0;
            StringBuffer sb = new StringBuffer();
            String bb = sc.next();
            StringBuffer sb1 = new StringBuffer(bb);
            String b = sb1.reverse().toString();
            String cc = sc.next();
            StringBuffer sb2 = new StringBuffer(cc);
            String c = sb2.reverse().toString();
            char[] arrb = b.toCharArray();
            char[] arrc = c.toCharArray();
            int bn = arrb.length;
            int cn = arrc.length;
            if(bn>=cn) {
                for(int j = 0;;j++) {
                    if(j>=bn) {
                        if(mark==1) {
                            sb.append(1);
                        }
                        break;
                    }
                    if(j<cn) {
                        int sum = ((int)arrb[j]-48)+((int)arrc[j]-48);
                        if(mark == 1) {
                            sum++;
                        }
                        if(sum<10) {
                            mark = 0;
                            sb.append(sum);
                        }else {
                            mark = 1;
                            sb.append(sum%10);
                        }
                    }else {
                        int sum = (int)arrb[j]-48;
                        if(mark==1) {
                            sum++;
                            mark = 0;
                            if(sum == 10) {
                                mark = 1;
                                sum  = 0;
                            }
                        }
                        sb.append(sum);
                    }
                }
            }
            else {
                for(int j = 0;;j++) {
                    if(j>=cn) {
                        if(mark==1) {
                            sb.append(1);
                        }
                        break;
                    }
                    if(j<bn) {
                        int sum = ((int)arrb[j]-48)+((int)arrc[j]-48);
                        if(mark == 1) {
                            sum++;
                        }
                        if(sum<10) {
                            mark = 0;
                            sb.append(sum);
                        }else {
                            mark = 1;
                            sb.append(sum%10);
                        }
                    }else {
                        int sum = (int)arrc[j]-48;
                        if(mark==1) {
                            sum++;
                            mark = 0;
                            if(sum==10) {
                                mark = 1;
                                sum = 0;
                            }
                        }
                        sb.append(sum);
                    }
                }
                
            }
            System.out.println("Case "+i+":");
            System.out.println(bb+" + "+cc+" = "+sb.reverse().toString());
            if(i!=a) {
                System.out.println();
            }
            
        }
        sc.close();
    }
}
原文地址:https://www.cnblogs.com/fightKun/p/9537788.html