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 −106≤a,b≤106. 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
翻译一下:就是让你每隔三位数字加一个逗号
ps:这个系列都是用Java做的
code:
import java.util.Scanner;
import static java.lang.Math.abs;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int sum = a+b;
int c = abs(sum);
if(sum<1000 && sum >-1000){
System.out.println(sum);
}else if(sum<100000 && sum > -1000000){
System.out.println(sum/1000 + "," +c%1000);
}else {
System.out.println(sum/100000 + "," +c%1000000/1000+ ","+c%1000) ;
}
}
}
最后这个解法得了15分还有几个测试点没通过。我也不知道哪里还有可以优化的地方,有大佬知道麻烦告诉我一下。