码迷,mamicode.com
首页 > 其他好文 > 详细

A+B Format

时间:2020-09-24 00:03:11      阅读:34      评论:0      收藏:0      [点我收藏+]

标签:file   numbers   bsp   void   java   rgs   imp   next   its   

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

标签:file   numbers   bsp   void   java   rgs   imp   next   its   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!