目录
- 1001 A+B Format (20分)
- 思路一:不按他们的位数分情况处理
- 代码一:用可变数组ArrayList+字符串截取方法subString
- 代码二:栈Stack+字符串截取方法subString
- 思路二:因为a和b的范围较小,所以可以按他们的位数分情况处理
- 代码一:字符串截取方法substring
- 代码二:StringBuilder的insert方法
- 代码三:格式化输出 printf
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 − 1 0 6 10^6 106 ≤a,b≤ 1 0 6 10^6 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
思路一:不按他们的位数分情况处理
我将两种代码合并在了一起,大家进代码看注释
代码一:用可变数组ArrayList+字符串截取方法subString
代码二:栈Stack+字符串截取方法subString
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
public class APlusB1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
sc.close();
System.out.print(add(a, b));
}
public static String add(int a, int b) {
String sum = a + b + "";
if (a + b > 0) {
if (sum.length() <= 3) {
return sum;
} else {
return addFormat(sum);
}
} else if (a + b < 0) {
sum = sum.substring(1);//取负号之后的数
if (sum.length() <= 3) {
return "-" + sum;
} else {
return "-" + addFormat(sum);
}
} else {
return "0";
}
}
public static String addFormat(String sum) {
int max = sum.length();
String str;
String str1;
StringBuilder str2 = new StringBuilder();
ArrayList<String> arrayList = new ArrayList<String>();
do {//因为 sum.length() > 3,才会到该方法,所以要先进行一次运算,而不是先判断
str = sum.substring(max - 3, max);
str1 = sum.substring(0, max - 3);
arrayList.add("," + str);
max = max - 3;
} while (max > 3);
for (int i = arrayList.size() - 1; i >= 0; i--) {
str2.append(arrayList.get(i));
}
return str1 + str2;
}
}
思路二:因为a和b的范围较小,所以可以按他们的位数分情况处理
代码一:字符串截取方法substring
import java.util.Scanner;
public class APlusB3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
sc.close();
System.out.print(addFormat(a, b));
}
public static String addFormat(int a, int b) {
String sum = a + b + "";
String str = "";
if (a + b < 0) { //处理负数
str = "-"; //尽量少用“+”, str += "-",因为会创建SringBuilder对象,增加运行时间
sum = sum.substring(1); //sum = ""+(0-(a+b))
}
int length = sum.length();
if (length >= 7) {
str += sum.substring(0, length - 6) + "," + sum.substring(length - 6, length - 3) + "," + sum.substring(length - 3, length);
} else if (length >= 4) {
str += sum.substring(0, length - 3) + "," + sum.substring(length - 3, length);
} else {
str += sum;
}
return str;
}
}
代码二:StringBuilder的insert方法
import java.util.Scanner;
public class APlusB4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
sc.close();
System.out.print(addFormat(a, b));
}
public static StringBuilder addFormat(int a, int b) {
StringBuilder sum = new StringBuilder(a + b + "");
String negative = "";
if (a + b < 0) { //处理负数
negative = "-"; //尽量少用“+”, negative += "-",因为会创建SringBuilder对象,增加运行时间
sum = new StringBuilder("" + (0 - (a + b))); //去掉负号
}
int length = sum.length();
if (length >= 7) {//其实length最大为7,因为a和b最大为10的6次方
sum.insert(length - 3, ",");
sum.insert(length - 6, ",");
} else if (length >= 4) {
sum.insert(length - 3, ",");
}
return sum.insert(0, negative);
}
}
代码三:格式化输出 printf
%03d
表示显示为三位十进进制数
d表示十进制数,3表示显示长度,
0表示不足三位的前补0
import java.util.Scanner;
public class APlusB2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
scanner.close();
int sum = a + b;
if (sum < 0) {
System.out.print("-");
sum = 0 - sum;
}
if (sum >= 1000000) {
System.out.printf("%d,%03d,%03d", sum / 1000000, sum % 1000000 / 1000, sum % 1000);
//%03d表示显示为三位十进进制数 d表示十进制数,3表示显示长度,0表示不足三位的前补0
} else if (sum >= 1000) {
System.out.printf("%d,%03d", sum / 1000, sum % 1000);
} else {
System.out.println(sum);
}
}
}