A+B Format

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 −. 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
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner input = new Scanner(System.in);
 6         int a = input.nextInt();
 7         int b = input.nextInt();
 8         input.close();
 9         int sum = a + b;
10         if (sum < 0) {
11             System.out.print("-");
12             sum = 0 - sum;
13         }
14         String str = String.valueOf(sum);
15         int length = str.length();
16         StringBuffer stringBuffer;
17         if (length <= 3) {
18             System.out.print(str);
19         } else if (length <= 6) {
20             stringBuffer = new StringBuffer(str);
21             stringBuffer.insert(length - 3, ",");
22             System.out.println(stringBuffer.toString());
23         } else {
24             stringBuffer = new StringBuffer(str);
25             stringBuffer.insert(length - 3, ",");
26             stringBuffer.insert(length - 6, ",");
27             System.out.println(stringBuffer.toString());
28         }
29     }
30 }

 

posted @ 2020-09-22 23:14  王余阳  阅读(214)  评论(0)    收藏  举报