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

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(",");
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,211评论 0 13
  • 鸠鸠moco阅读 1,576评论 0 0
  • KVM 安全 SMEP(supervision Mode Execution Protection)监督模式执行保...
    jeepshen阅读 4,202评论 0 1
  • 17年前写的文字,现在读起来,又是一番滋味 已经太久没有写点文字了,其间有很多次想把一些点滴记录下来,却因自己的懒...
    visionarywind阅读 3,759评论 0 1

友情链接更多精彩内容