甲级1001 A+B Format (20 分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32314965/article/details/84799331

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

 题目的意思是两个大于10的-6次方小于10的6次方的数相加,输出结果,注意结果要以有千分符的格式输出。可以用DecimalFormat类对格式进行修改。

Java的代码实现:

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);		
		int a = sc.nextInt();
		int b = sc.nextInt();
		System.out.println(new DecimalFormat(",##0").format(a+b));
	}
}

猜你喜欢

转载自blog.csdn.net/qq_32314965/article/details/84799331