美文网首页
PAT甲级JAVA版 1001 A+B Format(20分)

PAT甲级JAVA版 1001 A+B Format(20分)

作者: 杨小码 | 来源:发表于2019-01-30 14:18 被阅读0次

    1001 A+B Format (20 分)

    Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

    Input Specification:

    Each input file contains one test case. Each case contains a pair of integers a and b where-10^{6}<a,b<10^{6}.The numbers are separated by a space.

    Output Specification:

    For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

    Sample Input:

    -1000000 9

    Sample Output:

    -999,991

    1001 A+B格式化(20分)

    计算A+B的结果并且以标准格式输出——即,输出的结果必须每三个数字用逗号分隔为一组(除非结果不足四个数字)。

    输入规格:

    每个输入文件包含一个测试用例。每个用例都包含一对整数a和b,其中-10^{6}<a,b<10^{6}。数字用空格分隔。

    输出规格:

    对于每个测试用例,您应该在一行中输出a和b的总和。总和必须用标准格式写。

    输入样例:

    -1000000 9

    输出样例:

    -999,991

    代码:

    import java.util.Scanner;
    
    public class Main {
        private static Scanner sc;
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int a;
            int b;
            String sum;
            sc = new Scanner(System.in);
            a = sc.nextInt();
            b = sc.nextInt();
            sc.close();
            sum = a + b + "";
            for (int i = 0; i < sum.length(); i++) {
                System.out.print(sum.substring(i, i + 1));
                if ("-".equals(sum.substring(i, i + 1)))
                    continue;
                if ((i + 1) % 3 == sum.length() % 3 && i != sum.length() - 1)
                    System.out.print(",");
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:PAT甲级JAVA版 1001 A+B Format(20分)

          本文链接:https://www.haomeiwen.com/subject/xqoojqtx.html