首页 > 其他 > 详细

A+B Format

时间:2020-09-23 00:09:04      阅读:67      评论:0      收藏:0      [点我收藏+]

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 }

 

A+B Format

原文:https://www.cnblogs.com/0error0warning/p/13715487.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!